Android 时间对象操作工具类

Android 时间对象操作工具类

开发过程中,用到的一些比较有用的时间处理方法封装。


import android.text.TextUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author anothersunset
 * @create 2020/7/23
 * @Describe 时间操作工具类
 */
public class DateFormatUtil {

	/**
     * 时间戳转日期
     *
     * @param ms
     * @return
     */
    public static String transForDateStr(Integer ms) {
        if (ms == null) {
            ms = 0;
        }
        if (ms == 0) {
            return "";
        }
        long msl = (long) ms * 1000;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date temp = null;
        if (ms != null) {
            return sdf.format(msl);
        }
        return "";
    }

  /**
     * 日期转时间戳
     *
     * @param date
     * @return
     */
    public static Integer transForMilliSecond(Date date) {
        if (date == null) return null;
        return (int) (date.getTime() / 1000);
    }

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static Integer currentTimeStamp() {
        return (int) (System.currentTimeMillis() / 1000);
    }

   /**
     * 日期字符串转时间戳
     *
     * @param dateStr
     * @return
     */
    public static Integer transForMilliSecond(String dateStr, String format) {
        if (TextUtils.isEmpty(dateStr)) {
            return 0;
        }
        Date date = DateFormatUtil.formatDate(dateStr, format);
        return date == null ? null : DateFormatUtil.transForMilliSecond(date);
    }
    
   /**
     * 时间戳格式化输出
     *
     * @param ms     时间戳
     * @param format 格式化
     * @return
     */
    public static String transForDate(Integer ms, String format) {
        String str = "";
        if (ms != null) {
            long msl = (long) ms * 1000;
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            if (!ms.equals(0)) {
                try {
                    str = sdf.format(msl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }

 /**
     * 计算两个日期之间差的天数
     *
     * @param ts1 时间戳1
     * @param ts2 时间戳2
     * @return
     */
    public static int caculate2Days(Integer ts1, Integer ts2) {
        Date firstDate = DateFormatUtil.transForDate(ts1);
        Date secondDate = DateFormatUtil.transForDate(ts2);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(firstDate);
        int dayNum1 = calendar.get(Calendar.DAY_OF_YEAR);
        calendar.setTime(secondDate);
        int dayNum2 = calendar.get(Calendar.DAY_OF_YEAR);
        return Math.abs(dayNum1 - dayNum2);
    }

 /**
     * 获取传入时间和当前时间的时间差
     *
     * @return
     */
    public static Long getTimediff(int timeStamp) {
        Date d1 = DateFormatUtil.transForDate(timeStamp);
        Date today = new Date();
        if (d1.getTime() < today.getTime()) {
            return null;
        }
        return (d1.getTime() - today.getTime()) / 1000;
    }

    /**
     * 当前时间加一年
     */
    public static String addYear(int startTime) {
        Date firstDate = DateFormatUtil.transForDate(startTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(firstDate);
        calendar.add(Calendar.YEAR, 1);
        String d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
        return d1;
    }


 /**
     * 计算当前时间差距
     *
     * @param beginTime s
     * @return
     */
    public static String caculateTime(int beginTime) {
        /// 时间戳转日期
        /// 刚刚(1分钟内)
        /// x分钟前 (1小时内)
        /// x小时前 (1天内)
        /// 昨天 HH:mm
        /// 前天 HH:mm
        /// x天前 (本月内)
        /// MM-dd (本年内且非本月)
        /// yyyy-MM-dd (非本年)
        //秒数

        // 异常数据
        if (beginTime == 0) {
            return "";
        }

        long now = currentTimeStamp();
        long second = now - beginTime;
        long minute = second / 60;
        long hour = minute / 60;
        //今天0点
        long zero = now - (now + 8 * 3600) % (60 * 60 * 24);//今天零点零分零秒的毫秒数

        if (beginTime >= zero) {
            //当天
            // 先判断是否 > 1h
            if (hour > 0) {
                return hour + "小时前";
            } else {
                // 判断 > 1m
                if (minute > 0) {
                    return minute + "分钟前";
                } else {
                    return "刚刚";
                }
            }
        } else {
            long beginTimeL = beginTime;
            beginTimeL *= 1000L;
            //今天之前
            //1.昨天之后
            if (beginTime >= (zero - (3600 * 24))) {
                SimpleDateFormat format = new SimpleDateFormat("昨天 HH:mm");
                return format.format(new Date(beginTimeL));
            } else if (beginTime >= zero - (3600 * 48)) {
                //前天之后
                SimpleDateFormat format = new SimpleDateFormat("前天 HH:mm");
                return format.format(new Date(beginTimeL));
            } else {
                //判断是否是本月
                Calendar c = Calendar.getInstance();
                //设置为1号,当前日期既为本月第一天
                c.set(Calendar.DAY_OF_MONTH, 1);
                //将小时至0
                c.set(Calendar.HOUR_OF_DAY, 0);
                //将分钟至0
                c.set(Calendar.MINUTE, 0);
                //将秒至0
                c.set(Calendar.SECOND, 0);
                //将毫秒至0
                c.set(Calendar.MILLISECOND, 0);
                if (beginTimeL >= c.getTimeInMillis()) {
                    //本月内
//                    return (hour / 24 + (hour%24 == 0? 0 : 1)) + "天前";
                    return hour / 24 + "天前";
                } else {
                    //设置为1月1号
                    c.set(Calendar.MONTH, 1);
                    //设置为1号,当前日期既为本月第一天
                    c.set(Calendar.DAY_OF_MONTH, 1);
                    //将小时至0
                    c.set(Calendar.HOUR_OF_DAY, 0);
                    //将分钟至0
                    c.set(Calendar.MINUTE, 0);
                    //将秒至0
                    c.set(Calendar.SECOND, 0);
                    //将毫秒至0
                    c.set(Calendar.MILLISECOND, 0);
                    if (beginTimeL >= c.getTimeInMillis()) {
                        //本年内
                        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
                        return format.format(new Date(beginTimeL));
                    } else {
                        //非本年
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        return format.format(new Date(beginTimeL));
                    }
                }
            }

        }
    }

 /**
     * 获取某一年下半年的开始时间
     *
     * @return
     */
    public static Integer getHalfYearStartTime(String year) {
        return transForMilliSecondByTim(year + "-07-01 00:00:00", "yyyy-MM-dd HH:mm:ss");
    }

   /**
     * 某年某月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFisrtDayOfMonth(String year, String month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, Integer.parseInt(year.replace("年", "")));
        //设置月份
        cal.set(Calendar.MONTH, Integer.parseInt(month.replace("月", "")) - 1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth + " 00:00:00";
    }

  /**
     *  某年某月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(String year, String month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, Integer.parseInt(year.replace("年", "")));
        //设置月份
        cal.set(Calendar.MONTH, Integer.parseInt(month.replace("月", "")) - 1);
        //获取某月最小天数
        int firstDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth + " 23:59:59";
    }

 /**
     * 计算两个时间差多少天
     *
     * @param startTime
     * @param endTime
     * @param format
     * @return
     */
    public static int calcTime(int startTime, int endTime, String format) {
        String str1 = transForDate(startTime, format);
        String str2 = transForDate(endTime, format);
        return differentDaysByMillisecond(DateFormatUtil.formatDate(str1, format), DateFormatUtil.formatDate(str2, format));
    }

}

你可能感兴趣的:(android)