【Java日期时间处理工具类】

提供时间,字符串转换.时间间隔计算,最大时间,最小时间等

package zuoan.utils;

import com.chang.util.ValidateUtil;
import org.apache.commons.lang.time.DateFormatUtils;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static String YEAR = "yyyy";
    public static String YEARMONTH = "yyyy-MM";
    public static String YEARMONTHDAY = "yyyy-MM-dd";
    public static String YEARMONTHDAYTIMEBRANCH = "yyyy-MM-dd HH:mm";
    public static String YEARMONTHDAYTIMEBRANCHSECOND = "yyyy-MM-dd HH:mm:ss";
    public static String YEARMONTHDAYTIMEBRANCHSECONDMILLISECOND = "yyyy-MM-dd HH:mm:ss SSS";
    private static long differDay = 1000 * 24 * 60 * 60;
    private static long differHour = 1000 * 60 * 60;
    private static long differMinute = 1000 * 60;
    private static long differSecond = 1000;
    public static String DIFFERTIME = "0分钟";

    /**
     * 获取当前时间戳 yyyy-MM-dd HH:mm:ss
     * @return String
     */
    public static String getCurrentTime(){
        return DateFormatUtils.format(new Date(),YEARMONTHDAYTIMEBRANCHSECOND);
    }

    /**
     * 获取年
     * @return String
     */
    public static String getYear(Date date){
        return DateFormatUtils.format(date,YEAR);
    }

    /**
     * 获取年月
     * @return String
     */
    public static String getYearMonth(Date date){
        return DateFormatUtils.format(date,YEARMONTH);
    }

    /**
     * 获取年月日
     * @return String
     */
    public static String getYearMonthDay(Date date){
        return DateFormatUtils.format(date,YEARMONTHDAY);
    }

    /**
     * 获取年月日
     * @return String
     */
    public static String getYearMonthDayForResult(Date date){
        return DateFormatUtils.format(date,"yyyy年MM月dd日");
    }

    /**
     * 获取年月日时分
     * @return String
     */
    public static String getYearMonthDayTimeBranch(Date date){
        return DateFormatUtils.format(date,YEARMONTHDAYTIMEBRANCH);
    }

    /**
     * 获取年月日时分秒
     * @return String
     */
    public static String getYearMonthDayTimeBranchSecond(Date date){
        return DateFormatUtils.format(date,YEARMONTHDAYTIMEBRANCHSECOND);
    }

    /**
     * 获取当月日时分秒毫秒
     * @return String
     */
    public static String getYearMonthDayTimeBranchSecondMillisecond(Date date){
        return DateFormatUtils.format(date,YEARMONTHDAYTIMEBRANCHSECONDMILLISECOND);
    }

    /**
     * 将时间转换成固定格式字符串
     * @param date 时间
     * @param pattern 时间格式
     * @return String
     */
    public static String getStringByDate(Date date,String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = dateToDateTime(date);
        return formatter.format(localDateTime);
    }

    /**
     * 将字符串转换成固定格式时间 yyyy-MM-dd
     * @param string 时间字符串
     * @return Date
     */
    public static Date getDateByString(String string) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(string, dateTimeFormatter);
        return localDateToDate(localDate);
    }

    /**
     * 将字符串转换成固定格式时间 yyyy-MM-dd HH:mm:ss
     * @param string 时间字符串
     * @return Date
     */
    public static Date getDateTimeByString(String string) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);
        return dateTimeToDate(localDateTime);
    }

    /**
     * 获取某天最小时间
     * @param date 时间
     * @return Date
     */
    public static Date getDateMinTime(Date date) {
        LocalDateTime localDateTime = dateToDateTime(date);
        localDateTime = localDateTime.with(LocalTime.MIN);
        return dateTimeToDate(localDateTime);
    }

    /**
     * 获取某天最大时间
     * @param date 时间
     * @return Date
     */
    public static Date getDateMaxTime(Date date) {
        LocalDateTime localDateTime = dateToDateTime(date);
        localDateTime = localDateTime.with(LocalTime.MAX);
        return dateTimeToDate(localDateTime);
    }

    /**
     * 获取日期之后的某一天时间
     * @param date 时间
     * @param days 天数
     * @return Date
     */
    public static Date getAfterDaysDateTime(Date date, int days) {
        LocalDateTime localDateTime = dateToDateTime(date);
        localDateTime = localDateTime.plusDays(days);
        return dateTimeToDate(localDateTime);
    }

    /**
     * 获取日期之前的某一天时间
     * @param date 时间
     * @param days 天数
     * @return Date
     */
    public static Date getBeforeDaysDateTime(Date date, int days) {
        LocalDateTime localDateTime = dateToDateTime(date);
        localDateTime = localDateTime.minusDays(days);
        return dateTimeToDate(localDateTime);
    }

    /**
     * 获取日期月份第一天 00:00:00 000
     * @param date 时间
     * @return Date
     */
    public static Date getFirstDayOfMonth(Date date) {
        LocalDateTime localDateTime = dateToDateTime(date);
        localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
        return dateTimeToDate(localDateTime);
    }

    /**
     * 获取日期月份最后一天 23:59:59 999
     * @param date 时间
     * @return Date
     */
    public static Date getLastDayOfMonth(Date date) {
        LocalDateTime localDateTime = dateToDateTime(date);
        localDateTime = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
        return dateTimeToDate(localDateTime);
    }

    /**
     * 两个日期相差多少个月
     * @param dateBefore 开始时间
     * @param dateAfter 结束时间
     * @return Long
     */
    public static Long getUntilMonthByTwoDate(Date dateBefore, Date dateAfter) {
        LocalDate localDate1 = dateToLocalDate(dateBefore);
        LocalDate localDate2 = dateToLocalDate(dateAfter);
        return ChronoUnit.MONTHS.between(localDate1, localDate2);
    }

    /**
     * 两个日期相差多少天
     * @param dateBefore 开始时间
     * @param dateAfter 结束时间
     * @return Long
     */
    public static Long getUntilDayByTwoDate(Date dateBefore, Date dateAfter) {
        LocalDate localDate1 = dateToLocalDate(dateBefore);
        LocalDate localDate2 = dateToLocalDate(dateAfter);
        return ChronoUnit.DAYS.between(localDate1, localDate2);
    }

    /**
     * 两个日期相差多少小时
     * @param dateBefore 开始时间
     * @param dateAfter 结束时间
     * @return Long
     */
    public static Long getUntilHoursByTwoDate(Date dateBefore, Date dateAfter) {
        LocalDateTime localDate1 = dateToDateTime(dateBefore);
        LocalDateTime localDate2 = dateToDateTime(dateAfter);
        Long second = Duration.between(localDate1, localDate2).get(ChronoUnit.SECONDS);
        return second / 3600;
    }

    /**
     * 两个日期相差多少秒
     * @param dateBefore 开始时间
     * @param dateAfter 结束时间
     * @return Long
     */
    public static Long getUntilHoursByTwoSecond(Date dateBefore, Date dateAfter) {
        LocalDateTime localDate1 = dateToDateTime(dateBefore);
        LocalDateTime localDate2 = dateToDateTime(dateAfter);
        return Duration.between(localDate1, localDate2).get(ChronoUnit.SECONDS);
    }

    /**
     * LocalDateTime转换为Date
     * @param localDateTime 时间
     * @return Date
     */
    public static Date dateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * Date转换为LocalDateTime
     * @param date 时间
     * @return LocalDateTime
     */
    public static LocalDateTime dateToDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * Date转换为LocalDate
     * @param date 时间
     * @return LocalDate
     */
    public static LocalDate dateToLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * LocalDate转换为Date
     * @param localDate 时间
     * @return Date
     */
    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取增加天数后的日期
     * @param date 时间
     * @param days 天数
     * @return Date
     */
    public static Date getAddDay(Date date,Integer days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return calendar.getTime();
    }

    /**
     * 获取当前时间
     * @return Date
     */
    public static Date getCurDate() {
        return new Date();
    }

    /**
     * 获取两个时间相差时间
     * @param beforeDate 之前的时间
     * @param endDate 之后的时间
     * @return String
     */
    public static String getStringDateInfoByTwoDate(Date beforeDate,Date endDate) {
        long diff = endDate.getTime() - beforeDate.getTime();//获得两个时间的毫秒时间差异
        long day = diff / differDay;//计算差多少天
        long hour = diff % differDay / differHour;//计算差多少小时
        long min = diff % differDay % differHour / differMinute;//计算差多少分钟
        long second = diff % differDay % differHour % differMinute / differSecond;//计算差多少秒
        String string = "";
        if(0 != day){
            string = string + day + "天";
        }
        if(0 != hour){
            string = string + hour + "小时";
        }
        if(0 != min){
            string = string + min + "分钟";
        }
        if(ValidateUtil.isEmpty(string) && second > 0){//差距小于一分钟,计算秒
            string = second + "秒";
        }
        if(ValidateUtil.isEmpty(string)){//没有差距则为0分钟
            string = DIFFERTIME;
        }
        return string;
    }

    /**
     * 获取给定年月的最大最小日期
     * @param string yyyy-MM
     * @param flag true 最小日期 false 最大日期
     * @return String
     */
    public static String getMonthDayOneAndLast(String string,boolean flag){
        Integer year = Integer.parseInt(string.split("-")[0]);
        Integer month = Integer.parseInt(string.split("-")[1]);
        if(flag){
            return DateUtil.getDayByFlagOfMonth(year, month, true);
        }
        return DateUtil.getDayByFlagOfMonth(year, month, false);
    }

    /**
     * 获取给定年月的第一天和最后一天
     * @param year 年
     * @param month 月
     * @param flag true 第一天,false 最后一天
     * @return String
     */
    public static String getDayByFlagOfMonth(int year,int month,boolean flag){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);//设置年份
        calendar.set(Calendar.MONTH, month - 1);//设置月份
        if(flag){
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));//设置日历中月份的天数
        } else {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));//设置日历中月份的天数
        }
        return getYearMonthDay(calendar.getTime());//格式化日期
    }

    /**
     * 计算传入时间是否超时
     * @param longDate 传入时间
     * @param intervalTime 规定时间 秒
     * @return boolean
     */
    public static boolean isTimeOut(Long longDate,Long intervalTime){
        return new Date(System.currentTimeMillis()).getTime() - new Date(longDate).getTime() / 1000 > intervalTime;
    }
}

如果觉得文章对您有用,不妨 关注 + 收藏,更多使用文章在主页!!

你可能感兴趣的:(【Java工具常用类】,java,开发语言)