Java工具类-日期相关常用方法

91 阅读1分钟

日期工具类

/**
 * plusYears(i); // 加上几年
 * minusYears(i); // 减去几年
 * plusWeeks(i); // 加上几周
 * minusWeeks(i); // 减去几周
 * plusMonths(i); // 加上几月
 * minusMonths(i); // 减去几月
 * plusDays(i); // 加上几天
 * minusDays(i); // 减去几天
 */
public class DateUtils {

    // 当前日期
    public static LocalDate getNowDate() {
        return LocalDate.now();
    }

    // 当前时间
    public static LocalDateTime getNowDateTime() {
        return LocalDateTime.now();
    }

    // 昨天
    public static LocalDate getYesterday() {
        return getNowDate().minusDays(1);
    }

    // 本周的周一
    public static LocalDate getMonday() {
        return getNowDate().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
    }

    // 本周的周日
    public static LocalDate getSunday() {
        return getNowDate().with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
    }

    // 本月第一天
    public static LocalDate getFirstDayOfMonth() {
        return getNowDate().with(TemporalAdjusters.firstDayOfMonth());
    }

    // 本月最后一天
    public static LocalDate getLastDayOfMonth() {
        return getNowDate().with(TemporalAdjusters.lastDayOfMonth());
    }

    // 当前季度第一天
    public static LocalDate getFirstDayOfQuarter() {
        return getFirstOrLastDayOfQuarter(true);
    }

    // 当前季度最后一天
    public static LocalDate getLastDayOfQuarter() {
        return getFirstOrLastDayOfQuarter(false);
    }

    /**
     * 季度开始或结束一天
     *
     * @param isFirst true 季度开始 false 季度结束
     * @return {@link LocalDate}
     */
    public static LocalDate getFirstOrLastDayOfQuarter(Boolean isFirst) {
        LocalDate today = LocalDate.now();
        LocalDate resDate;
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
        }
        return resDate;
    }
    
    /**
     * Date转LocalDate
     *
     * @param date 日期
     * @return {@link LocalDate}
     */
    public static LocalDate dateConvertLocalDate(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        return localDateTime.toLocalDate();
    }

    /**
     * Date转LocalDateTime
     *
     * @param date 日期
     * @return {@link LocalDateTime}
     */
    public static LocalDateTime dateConvertLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * LocalDate转Date
     *
     * @param localDate 当地日期
     * @return {@link Date}
     */
    public static Date localDateConvertDate(LocalDate localDate) {
       return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

   /**
     * 获取两个日期之间的日期 从大到小
     */
    public static List<LocalDate> getDescDateList(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> result = new ArrayList<>();
        if (endDate.compareTo(startDate) < 0) {
            return result;
        }
        while (true) {
            result.add(endDate);
            if (endDate.compareTo(startDate) <= 0) {
                break;
            }
            endDate = endDate.plusDays(-1);
        }
        return result;
    }

    /**
     * 获取两个日期之间的日期  从小到大
     */
    public static List<LocalDate> getAscDateList(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> result = new ArrayList<>();
        if (endDate.compareTo(startDate) < 0) {
            return result;
        }
        while (true) {
            result.add(startDate);
            if (startDate.compareTo(endDate) >= 0) {
                break;
            }
            startDate = startDate.plusDays(1);
        }
        return result;
    }
}