关于时间的操作

获取当前年份
public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }
获取当前月份
public static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }
计算2个日期相差多少小时
 public static double dateTimeDiff(Date d1, Date d2) {
        return (double) dateMilliSecondDiff(d1, d2) / (double) ONE_HOUR_MIL_SEC;
    }
计算2个日期相差多少分钟 
public static Double dateTimeDiffMinute(Date d1, Date d2) {
        return (double) dateMilliSecondDiff(d1, d2) / (double) ONE_MINUTE_MIL_SEC;
    }
计算2个日期相差多少天
 public static double dayTimeDiff(Date d1, Date d2) {
        return (double) dateMilliSecondDiff(d1, d2) / (double) ONE_DAY_MILSEC;
    }
计算2个日期相差多少秒
public static double secondTimeDiff(Date d1, Date d2) {
        assertDateParamsRequired(d1, d2);
        return (double) dateMilliSecondDiff(d1, d2) / (double) ONE_SECOND_MIL_SEC;
    }
判断两个日期是否相差几年
* @param d1    开始日期
* @param d2 结束日期
* @param years 年数
* @return true 表示相差小于年数 ,false 表示相差大于年数,精确度为天
public static boolean isDateTimeDiff(Date d1, Date d2, int years) {
        assertDateParamsRequired(d1, d2);
        Calendar testTimeCalendar = Calendar.getInstance();
        testTimeCalendar.setTime(d1);
        testTimeCalendar.add(Calendar.YEAR, years);
        return DateTimeUtil.dayTimeDiff(d2, testTimeCalendar.getTime()) < 1;
    }
计算2个日期相差多少 天
public static long dateDayDiff(Date d1, Date d2) {
        long millis1 = d1.getTime() + FIRST_DAY_OFFSET_MILSEC;
        long millis2 = d2.getTime() + FIRST_DAY_OFFSET_MILSEC;
        long day1 = millis1 / ONE_DAY_MILSEC;
        long day2 = millis2 / ONE_DAY_MILSEC;
        return day1 - day2;
    }
计算2个日期相差毫秒数
public static long dateMilliSecondDiff(Date d1, Date d2) {
        long millis1 = d1.getTime();
        long millis2 = d2.getTime();
        return millis1 - millis2;
    }
返回年月日格式 yyyy-MM-DD 其他格式直接 修改格式化后的样式即可
*
* @param date
* @return
 public static String getYMD(Date date) {
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
        return formate.format(date);
    }
解析String为Date<br>
* String的格式要求为"yyyy-MM-dd HH:mm:ss"<br>
 public static Date parse2Date(String timeDate) {

        SimpleDateFormat formate = new SimpleDateFormat(DATE_FORMAT_STRING);
        Date date;
        try {
            date = formate.parse(timeDate);
            return date;
        } catch (ParseException e) {
            return null;
        }
    }
判断当前日期是否为该月的最后一天
public static boolean isLastDayOfMonth(Date date) {
        Calendar cald = Calendar.getInstance();
        cald.setTime(date);
        int today = cald.get(Calendar.DAY_OF_MONTH);
        int lastDayOfMonth = cald.getMaximum(Calendar.DAY_OF_MONTH);
        return today == lastDayOfMonth;

    }
根据参数指定的年月日信息生成Date对象,其时分秒、毫秒均为0<br>
public static Date getDate(int year, int month, int date) {

        if (month < 1 || month > ONE_YEAR_MONTH_TOTAL) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, date, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }
接受一个日期,然后返回这个日期所属的日历月的第一天。 举例来说,如果传入"2012-06-15", 那么返回结果是"2012-06-01"
public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date == null ? new Date() : date);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
获取传入时间偏移n 后的时间
*
* @param date
* @param n 偏移天数 可负数
* @return
 public static Date getDayDiff(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date == null ? new Date() : date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + n);
        return calendar.getTime();
    }
用指定的时分秒替换指定日期的时分秒
@param date 指定的日期
* @param time 时分秒,格式[HH:mm:ss],例如23:59:59,09:01:01
* @return 替换后的日期,如果转化日期异常,返回转化前的日期
public static Date appendDateAndTime(Date date, String time) {
        try {
            if (date == null || org.apache.commons.lang3.StringUtils.isBlank(time)) {
                return null;
            }
            String dateString = new SimpleDateFormat("yyyy-MM-dd").format(date);
            dateString = dateString.concat(BLANK).concat(time);
            return new SimpleDateFormat(DATE_FORMAT_STRING).parse(dateString);
        } catch (ParseException e) {
            return date;
        }
    }
获取前一天的开始时间 如果前一天小于当月第一天则时间为当月第一天的开始时间
public static Date lastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (date == null) {
            date = new Date();
        }
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (day == 1) {
            date = getFirstTimeOfDate(calendar.getTime());
        } else {
            date = getFirstTimeOfDate(addDays(calendar.getTime(), -1));
        }
        return date;
    }
计算两个毫秒值之间的分钟
public static String friendlyTime(Date time) {
        //获取time距离当前的秒数
        int ct = (int) ((System.currentTimeMillis() - time.getTime()) / 1000);

        if (ct == 0) {
            return "刚刚";
        }
        if (ct > 0 && ct < 60) {
            return ct + "秒前";
        }
        if (ct >= 60 && ct < 3600) {
            return Math.max(ct / 60, 1) + "分钟前";
        }
        if (ct >= 3600 && ct < 86400) {
            return ct / 3600 + "小时前";
        }
        //86400 * 30
        if (ct >= 86400 && ct < 2592000) {
            int day = ct / 86400;
            return day + "天前";
        }
        //86400 * 30
        if (ct >= 2592000 && ct < 31104000) {
            return ct / 2592000 + "月前";
        }
        return ct / 31104000 + "年前";
    }
 倒计时24小时 比如订单24未支付就自动取消 如果注释放开的话 就返回剩余“23小时14分钟”字样
public static long getDistanceTimes(Date date) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long diff;
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
        Date nowdate=calendar.getTime(); //这个时间就是日期往后推一天的结果
        diff = nowdate.getTime() - System.currentTimeMillis();
//        day = diff / (24 * 60 * 60 * 1000);
//        hour = (diff / (60 * 60 * 1000) - day * 24);
//        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
//        sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
//        long[] times = {day, hour, min, sec};
        return diff;
    }

 

posted @ 2019-02-21 15:59  逍遥devil  阅读(156)  评论(0)    收藏  举报