基于Java 8,新的日期和时间API
package com.boob.common.utils;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
/**
* @description:JAVA 8 时间工具类
* @author:boob
* @since:2020/2/9
*/
public class DateTimeUtils {
/**
* 格式:yyyy-MM-dd HH:mm:ss
*/
public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 格式:yyyy-MM-dd
*/
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
/**
* 格式:yyyy-MM
*/
public static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
/**
* 格式:HH:mm:ss
*/
public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
public DateTimeUtils() {
}
// ------------------------------------------- 获取时间 -------------------------------------------
/**
* 返回当前日期 + 时间
*
* @return yyyy-MM-dd HH:mm:ss
*/
public static LocalDateTime get() {
return LocalDateTime.now();
}
/**
* 返回当前的日期
*
* @return yyyy-MM-dd
*/
public static LocalDate getCurrentLocalDate() {
return LocalDate.now();
}
/**
* 返回当前时间
*
* @return HH:mm:ss
*/
public static LocalTime getCurrentLocalTime() {
return LocalTime.now();
}
/**
* 返回当前年份
*
* @return yyyy
*/
public static int getYear() {
return get().getYear();
}
/**
* 返回指定年份的当前日期 + 时间
*
* @return yyyy-MM-dd HH:mm:ss
*/
public static LocalDateTime withYear(int year) {
return get().withYear(year);
}
/**
* 返回指定月份
*
* @return MM
*/
public static int getMonth() {
return get().getMonthValue();
}
/**
* 返回指定年份开始时间
*
* @return MM
*/
public static LocalDateTime firstDayOfThisYear(int year) {
return withYear(year).with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
}
// ------------------------------------------- 时间格式化 -------------------------------------------
/**
* @param dateTimeStr : yyyy-MM-dd HH:mm:ss
* @description:yyyy-MM-dd HH:mm:ss
* @return:java.time.LocalDateTime
* @author:ray
* @create:2019-11-21 16:36
*/
public static LocalDateTime parseLocalDateTime(String dateTimeStr) {
return LocalDateTime.parse(dateTimeStr, DATETIME_FORMATTER);
}
/**
* @param dateStr : yyyy-MM-dd
* @description:yyyy-MM-dd
* @return:java.time.LocalDate
* @author:ray
* @create:2019-11-21 16:36
*/
public static LocalDate parseLocalDate(String dateStr) {
return LocalDate.parse(dateStr, DATE_FORMATTER);
}
/**
* @param timeStr :HH:mm:ss
* @description:HH:mm:ss
* @return:java.time.LocalTime
* @author:ray
* @create:2019-11-21 16:36
*/
public static LocalTime parseLocalTime(String timeStr) {
return LocalTime.parse(timeStr, TIME_FORMATTER);
}
/**
* @param date : LocalDate
* @description:yyyy-MM-dd
* @return:java.lang.String
* @author:ray
* @create:2019-11-21 16:36
*/
public static String formatLocalDate(LocalDate date) {
return date.format(DATE_FORMATTER);
}
/**
* @param datetime : LocalDateTime
* @description:yyyy-MM-dd HH:mm:ss
* @return:java.lang.String
* @author:ray
* @create:2019-11-21 16:36
*/
public static String formatLocalDateTime(LocalDateTime datetime) {
return datetime.format(DATETIME_FORMATTER);
}
/**
* @param time : LocalTime
* @description: HH:mm:ss
* @return:java.lang.String
* @author:ray
* @create:2019-11-21 16:36
*/
public static String formatLocalTime(LocalTime time) {
return time.format(TIME_FORMATTER);
}
// ------------------------------------------- 时间计算 -------------------------------------------
/**
* @param startDateInclusive : 开始时间
* @param endDateExclusive : 结束时间
* @description:日期相隔天数
* @return:int
* @author:ray
* @create:2019-12-04 16:00
*/
public static int periodDays(LocalDate startDateInclusive, LocalDate endDateExclusive) {
return Period.between(startDateInclusive, endDateExclusive).getDays();
}
/**
* @param startInclusive:开始时间
* @param endExclusive:结束时间
* @description:日期相隔小时
* @return:int
* @author:ray
* @create:2019-12-04 16:00
*/
public static long durationHours(Temporal startInclusive, Temporal endExclusive) {
return Duration.between(startInclusive, endExclusive).toHours();
}
/**
* @param startInclusive:开始时间
* @param endExclusive:结束时间
* @description:日期相隔分钟
* @return:int
* @author:ray
* @create:2019-12-04 16:00
*/
public static long durationMinutes(Temporal startInclusive, Temporal endExclusive) {
return Duration.between(startInclusive, endExclusive).toMinutes();
}
/**
* @param startInclusive:开始时间
* @param endExclusive:结束时间
* @description:日期相隔毫秒数
* @return:int
* @author:ray
* @create:2019-12-04 16:00
*/
public static long durationMillis(Temporal startInclusive, Temporal endExclusive) {
return Duration.between(startInclusive, endExclusive).toMillis();
}
/**
* @param year
* @return String
* @Title: getFirstDayOfThisYear
* @Description: 获取设置所属年最初时间
*/
public static String getFirstDayOfThisYear(int year) {
LocalDateTime firstDayOfThisYear = firstDayOfThisYear(year);
return DATETIME_FORMATTER.format(firstDayOfThisYear);
}
/**
* @param year
* @return String
* @Title: getFirstDayOfThisYear
* @Description: 获取设置所属年最后一天
*/
public static LocalDateTime lastDayOfThisYear(int year) {
return withYear(year).with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
}
/**
* @param year
* @return String
* @Title: getLastDayOfThisYear
* @Description: 获取设置所属年最后时间
*/
public static String getLastDayOfThisYear(int year) {
LocalDateTime lastDayOfThisYear = lastDayOfThisYear(year);
return DATETIME_FORMATTER.format(lastDayOfThisYear);
}
/**
* @return String
* @Title: getFirstDayOfThisMonth
* @Description: 获取本月的第一天(当前时间)
*/
public static String getFirstDayOfThisMonth() {
LocalDateTime firstDayOfThisYear = get().with(TemporalAdjusters.firstDayOfMonth());
return DATETIME_FORMATTER.format(firstDayOfThisYear);
}
/**
* @return String
* @Title: getFirstDayOfThisMonth
* @Description: 获取本月的最末天(当前时间)
*/
public static String getLastDayOfThisMonth() {
LocalDateTime firstDayOfThisYear = get().with(TemporalAdjusters.lastDayOfMonth());
return DATETIME_FORMATTER.format(firstDayOfThisYear);
}
/**
* @param days
* @return LocalDateTime
* @Title: plusDays
* @Description: 当前日期向后推多少天
*/
public static LocalDateTime plusDays(int days) {
return get().plusDays(days);
}
/**
* @return LocalDateTime
* @Title: todayStart
* @Description: 当天开始时间
*/
public static LocalDateTime todayStart() {
return LocalDateTime.of(getCurrentLocalDate(), LocalTime.MIN);
}
/**
* @return LocalDateTime
* @Title: todayEnd
* @Description: 当天结束时间
*/
public static LocalDateTime todayEnd() {
return LocalDateTime.of(getCurrentLocalDate(), LocalTime.MAX);
}
/**
* @param year
* @param month
* @return LocalDateTime
* @Title: firstDayOfWeekInYearMonth
* @Description: 获取指定年月的第一个周一
*/
public static LocalDateTime firstDayOfWeekInYearMonth(int year, int month) {
return get().withYear(year).withMonth(month).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
}
/**
* @return String
* @Title: getStartDayOfWeekToString
* @Description: 获取周第一天
*/
public static String getStartDayOfWeekToString() {
return formatLocalDate(getStartDayOfWeek());
}
public static LocalDate getStartDayOfWeek() {
TemporalAdjuster FIRST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate
.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));
return getCurrentLocalDate().with(FIRST_OF_WEEK);
}
/**
* @return String
* @Title: getEndDayOfWeekToString
* @Description: 获取周最后一天
*/
public static String getEndDayOfWeekToString() {
return formatLocalDate(getEndDayOfWeek());
}
public static LocalDate getEndDayOfWeek() {
TemporalAdjuster LAST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(
DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
return getCurrentLocalDate().with(LAST_OF_WEEK);
}
/**
* @param localDate : LocalDate 类型
* @description:LocalDate 转 Date
* @return:java.util.Date
* @author:ray
* @create:2019-12-12 12:16
*/
public static Date asDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
}
/**
* @param localDateTime : LocalDate 类型
* @description:LocalDateTime 转 Date
* @return:java.util.Date
* @author:ray
* @create:2019-12-12 12:16
*/
public static Date asDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* @param date : Date 类型
* @description:Date 转 LocalDate
* @return:java.time.LocalDate
* @author:ray
* @create:2019-12-12 12:16
*/
public static LocalDate asLocalDate(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* @param date : Date 类型
* @description:Date 转 LocalDateTime
* @return:java.time.LocalDateTime
* @author:ray
* @create:2019-12-12 12:16
*/
public static LocalDateTime asLocalDateTime(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
}
// public static void main(String[] args) {
// int year = 2018;
// int month = 5;
// System.out.println("---------- 获取时间 ----------");
// // 2019-12-04
// System.out.println(getCurrentLocalDate());
// // 16:11:56.545
// System.out.println(getCurrentLocalTime());
// // 2019-12-04T16:11:56.545
// System.out.println(DATETIME_FORMATTER.format(get()));
// // 2019
// System.out.println(getYear());
// // 2019-12-04T16:11:56.545
// System.out.println(withYear(year));
// // 12
// System.out.println(getMonth());
// // 2019-01-01T00:00
// System.out.println(firstDayOfThisYear(year));
//
// System.out.println("---------- 时间计算 ----------");
// // 2
// System.out.println(periodDays(getCurrentLocalDate(), getCurrentLocalDate().plusDays(2)));
// // 48
// System.out.println(durationHours(get(), get().plusDays(2)));
// // 120
// System.out.println(durationMinutes(get(), get().plusMinutes(120)));
// // 7200000
// System.out.println(durationMillis(get(), get().plusMinutes(120)));
// // 2018-01-01 00:00:00
// System.out.println(getFirstDayOfThisYear(year));
// // 2018-12-31 23:59:59
// System.out.println(getLastDayOfThisYear(year));
// // 2019-12-01 16:42:42
// System.out.println(getFirstDayOfThisMonth());
// // 2019-12-31 16:42:42
// System.out.println(getLastDayOfThisMonth());
// // 2019-12-05 16:42:42
// System.out.println(DATETIME_FORMATTER.format(plusDays(1)));
// // 2019-12-03 16:42:42
// System.out.println(DATETIME_FORMATTER.format(plusDays(-1)));
// // 2019-12-04T00:00
// System.out.println(todayStart());
// // 2019-12-04T23:59:59.999999999
// System.out.println(todayEnd());
// // 2018-05-07T16:51:10.576
// System.out.println(firstDayOfWeekInYearMonth(year, month));
// // 2019-12-02
// System.out.println(getStartDayOfWeekToString());
// // 2019-12-08
// System.out.println(getEndDayOfWeekToString());
// }
}