import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
/**
* @ClassName LocalDateUtils
* @Description java8日期时间处理工具类
* @Author duyq
* @Date 2019/7/31 15:39
**/
public final class LocalDateUtils {
//日期
/** char(8) */
public static final String YYYYMMDD = "yyyyMMdd";
/** char(10) */
public static final String YYYY_MM_DD = "yyyy-MM-dd";
/** char(10) */
public static final String YYYY_U_MM_U_DD = "yyyy年MM月dd日";
/** char(10) */
public static final String YYYY__MM__DD = "yyyy/MM/dd";
//日期时间
/** char(14) */
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
/** char(17) */
public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
/** char(17) */
public static final String YYYYMMDD_HH_MM_SS = "yyyyMMdd HH:mm:ss";
/** char(19) */
public static final String YYYY__MM__DD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss";
/** char(19) */
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/** char(19) */
public static final String YYYY_U_MM_U_DD_HH_MM_SS = "yyyy年MM月dd日 HH:mm:ss";
/** char(19) */
public static final String YYYY_U_MM_U_DD_HH_U_MM_U_SS = "yyyy年MM月dd日 HH时mm分ss";
/** char(20) */
public static final String YYYY_U_MM_U_DD_HH_U_MM_U_SS_U = "yyyy年MM月dd日 HH时mm分ss秒";
//时间
/** char(6) */
public static final String HHMMSS = "HHmmss";
/** char(8) */
public static final String HH_MM_SS = "HH:mm:ss";
/** char(8) */
public static final String HH_U_MM_U_SS = "HH时mm分ss";
/** char(9) */
public static final String HH_U_MM_U_SS_U = "HH时mm分ss秒";
//年、月、日
/** 年 */
public static final String UNIT_Y = "Y";
/** 月 */
public static final String UNIT_M = "M";
/** 日 */
public static final String UNIT_D = "D";
//毫秒数
/** 一天的毫秒数 */
private static final long ND = 1000 * 24 * 60 * 60;
/** 一小时的毫秒数 */
private static final long NH = 1000 * 60 * 60;
/** 一分钟的毫秒数 */
private static final long NM = 1000 * 60;
/** 一秒钟的毫秒数 */
private static final long NS = 1000;
/**
* @Description 获取当前日期
* @Author duyq
* @Param []
* @Date 16:08 2019/7/31
* @return java.time.LocalDate
**/
public static LocalDate getLocalDate(){
return LocalDate.now();
}
/**
* @Description 获取当前日期时间
* @Author duyq
* @Param []
* @Date 16:08 2019/7/31
* @return java.time.LocalDate
**/
public static LocalDateTime getLocalDateTime(){
return LocalDateTime.now();
}
/**
* @Description 获取当前日期
* @Author duyq
* @Param []
* @Date 16:08 2019/7/31
* @return java.time.LocalDate
**/
public static LocalTime getLocalTime(){
return LocalTime.now();
}
/**
* @Description 格式化当前日期
* @Author duyq
* @Param [format]
* @Date 15:48 2019/7/31
* @return java.lang.String
**/
public static String formatLocalDate(String format) {
return getLocalDate().format(DateTimeFormatter.ofPattern(format));
}
/**
* @Description 格式化当前日期时间
* @Author duyq
* @Param [format]
* @Date 16:02 2019/7/31
* @return java.lang.String
**/
public static String formatLocalDateTime(String format){
return getLocalDateTime().format(DateTimeFormatter.ofPattern(format));
}
/**
* @Description 格式化当前时间
* @Author duyq
* @Param [format]
* @Date 16:05 2019/7/31
* @return java.lang.String
**/
public static String formatLocalTime(String format){
return getLocalTime().format(DateTimeFormatter.ofPattern(format));
}
/**
* @Description 格式化指定的日期
* @Author duyq
* @Param [localDate, format]
* @Date 16:11 2019/7/31
* @return java.lang.String
**/
public static String getFormatLocalDate(LocalDate localDate,String format){
return localDate.format(DateTimeFormatter.ofPattern(format));
}
/**
* @Description 格式化指定的日期时间
* @Author duyq
* @Param [localDateTime, format]
* @Date 16:14 2019/7/31
* @return java.lang.String
**/
public static String getFormatLocalDateTime(LocalDateTime localDateTime,String format){
return localDateTime.format(DateTimeFormatter.ofPattern(format));
}
/**
* @Description 格式化指定的时间
* @Author duyq
* @Param [localTime, format]
* @Date 16:18 2019/7/31
* @return java.lang.String
**/
public static String getFormatLocalTime(LocalTime localTime,String format){
return localTime.format(DateTimeFormatter.ofPattern(format));
}
/**
* @Description java.util.Date 转换为 java.time.LocalDate 默认时区为东8区
* @Author duyq
* @Param [date]
* @Date 16:52 2019/7/31
* @return java.time.LocalDate
**/
public static LocalDate dateToLocalDate(Date date){
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* @Description java.util.Date 转换为 java.time.LocalDateTime,默认时区为东8区
* @Author duyq
* @Param [date]
* @Date 16:54 2019/7/31
* @return java.time.LocalDateTime
**/
public static LocalDateTime dateToLocalDateTime(Date date){
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
/**
* @Description java.time.LocalDate 转换为 java.util.Date,默认时区为东8区(时间默认00:00:00)
* @Author duyq
* @Param [localDate]
* @Date 16:57 2019/7/31
* @return java.util.Date
*/
public static Date localDateToDate(LocalDate localDate){
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
/**
* @Description java.time.LocalDateTime 转换为 java.util.Date,默认时区为东8区
* @Author duyq
* @Param [localDateTime]
* @Date 16:57 2019/7/31
* @return java.util.Date
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime){
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* @Description 获取当日 yyyy-MM-dd 00:00:00
* @Author duyq
* @Param []
* @Date 17:06 2019/7/31
* @return java.lang.String
**/
public static String getBeginDateTime() {
LocalDateTime localDateTime = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
return getFormatLocalDateTime(localDateTime,YYYY_MM_DD_HH_MM_SS);
}
/**
* @Description 获取当日 yyyy-MM-dd 23:59:59
* @Author duyq
* @Param []
* @Date 17:11 2019/7/31
* @return java.lang.String
**/
public static String getEndDateTime() {
LocalDateTime localDateTime = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999999999);
return getFormatLocalDateTime(localDateTime,YYYY_MM_DD_HH_MM_SS);
}
/**
* @Description 获取指定日期 yyyy-MM-dd 00:00:00
* @Author duyq
* @Param [localDate]
* @Date 17:10 2019/7/31
* @return java.lang.String
**/
public static String getBeginDateTime(LocalDateTime localDateTime) {
LocalDateTime dateTime = localDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
return getFormatLocalDateTime(dateTime,YYYY_MM_DD_HH_MM_SS);
}
/**
* @Description 获取指定日期 yyyy-MM-dd 23:59:59
* @Author duyq
* @Param [localDateTime]
* @Date 17:10 2019/7/31
* @return java.lang.String
**/
public static String getEndDateTime(LocalDateTime localDateTime) {
LocalDateTime dateTime = localDateTime.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
return getFormatLocalDateTime(dateTime,YYYY_MM_DD_HH_MM_SS);
}
/**
* @Description 获取当前年份
* @Author duyq
* @Param []
* @Date 17:24 2019/7/31
* @return int
**/
public static int getYear(){
return getLocalDate().getYear();
}
/**
* @Description 获取指定日期年份
* @Author duyq
* @Param []
* @Date 17:24 2019/7/31
* @return int
**/
public static int getYear(LocalDate localDate){
return localDate.getYear();
}
/**
* @Description 获取当前月份对象
* @Author duyq
* @Param []
* @Date 17:26 2019/7/31
* @return java.time.Month
**/
public static Month getMonth(){
return getLocalDate().getMonth();
}
/**
* @Description 获取指定日期的月份对象
* @Author duyq
* @Param []
* @Date 17:26 2019/7/31
* @return java.time.Month
**/
public static Month getMonth(LocalDate localDate){
return localDate.getMonth();
}
/**
* @Description 获取当前月份
* @Author duyq
* @Param []
* @Date 17:27 2019/7/31
* @return int
**/
public static int getMonthValue(){
return getLocalDate().getMonthValue();
}
/**
* @Description 获取指定日期的月份
* @Author duyq
* @Param []
* @Date 17:27 2019/7/31
* @return int
**/
public static int getMonthValue(LocalDate localDate){
return localDate.getMonthValue();
}
/**
* @Description 获取当前是星期几
* @Author duyq
* @Param []
* @Date 17:28 2019/7/31
* @return java.time.DayOfWeek
**/
public static DayOfWeek getDayOfWeek(){
return getLocalDate().getDayOfWeek();
}
/**
* @Description 获取指定日期是星期几
* @Author duyq
* @Param []
* @Date 17:28 2019/7/31
* @return java.time.DayOfWeek
**/
public static DayOfWeek getDayOfWeek(LocalDate localDate){
return localDate.getDayOfWeek();
}
/**
* @Description 获取当前是本月第几天
* @Author duyq
* @Param []
* @Date 17:29 2019/7/31
* @return int
**/
public static int getDayOfMonth() {
return getLocalDate().getDayOfMonth();
}
/**
* @Description 获取指定日期是本月第几天
* @Author duyq
* @Param []
* @Date 17:29 2019/7/31
* @return int
**/
public static int getDayOfMonth(LocalDate localDate) {
return localDate.getDayOfMonth();
}
/**
* @Description 获取当前是本年第几天
* @Author duyq
* @Param []
* @Date 17:30 2019/7/31
* @return int
**/
public static int getDayOfYear() {
return getLocalDate().getDayOfYear();
}
/**
* @Description 获取指定日期是本年第几天
* @Author duyq
* @Param []
* @Date 17:30 2019/7/31
* @return int
**/
public static int getDayOfYear(LocalDate localDate) {
return localDate.getDayOfYear();
}
/**
* @Description 指定格式的日期时间字符串转换为 java.time.LocalDateTime
* @Author duyq
* @Param [dateTime 待转日期时间字符串, formart 待转日期时间字符串的格式类型]
* @Date 17:38 2019/7/31
* @return java.time.LocalDateTime
**/
public static LocalDateTime getDateTime(String dateTime, String format) {
if (requireNonNull(dateTime) || requireNonNull(format)) {
return null;
}
return LocalDateTime.parse(dateTime.trim(),DateTimeFormatter.ofPattern(format));
}
/**
* @Description 指定格式的日期时间字符串转换为 java.time.LocalDate
* @Author duyq
* @param date 待转日期字符串
* @param format 待转日期字符串的格式
* @Date 2019/8/1 10:32
* @return java.time.LocalDate
**/
public static LocalDate getDate(String date, String format) {
if (requireNonNull(date) || requireNonNull(format)) {
return null;
}
return LocalDate.parse(date.trim(),DateTimeFormatter.ofPattern(format));
}
/**
* @Description 指定格式的日期时间字符串转换为 java.time.LocalTime
* @Author duyq
* @param time 待转日期时间字符串
* @param format 待转日期时间字符串的格式
* @Date 2019/8/1 10:31
* @return java.time.LocalTime
**/
public static LocalTime getTime(String time, String format) {
if (requireNonNull(time) || requireNonNull(format)) {
return null;
}
return LocalTime.parse(time.trim(),DateTimeFormatter.ofPattern(format));
}
/**
* @Description 判断是否为空或空字符,当前类不能信息不能为空字符
* @Author duyq
* @Param [str]
* @Date 18:02 2019/7/31
* @return boolean
**/
private static boolean requireNonNull(String str) {
return str == null || str.trim().isBlank();
}
/**
* @Description 将要增加或删减的年、月、日、时、分、秒,参数为正数
* @Author duyq
* @Param [localDate, year, month, day]
* @Date 18:07 2019/7/31
* @return java.time.LocalDate
**/
public static LocalDate plusLocalDate(LocalDate localDate, long year, long month, long day) {
localDate = localDate.plusYears(year);
localDate = localDate.plusMonths(month);
localDate = localDate.plusDays(day);
return localDate;
}
/**
* @Description 将要增加的年、月、日、时、分、秒,参数为正数
* @Author duyq
* @Param [localDateTime, year, month, day, hour, minute, second]
* @Date 18:05 2019/7/31
* @return java.time.LocalDateTime
**/
public static LocalDateTime plusLocalDateTime(LocalDateTime localDateTime, long year, long month, long day,
long hour, long second, long minute) {
localDateTime = localDateTime.plusYears(year);
localDateTime = localDateTime.plusMonths(month);
localDateTime = localDateTime.plusDays(day);
localDateTime = localDateTime.plusHours(hour);
localDateTime = localDateTime.plusSeconds(second);
localDateTime = localDateTime.plusMinutes(minute);
return localDateTime;
}
/**
* @Description 将要删减的年、月、日、时、分、秒,参数为正数
* @Author duyq
* @Param [localDate, year, month, day, hour, minute, second]
* @Date 18:05 2019/7/31
* @return java.time.LocalDate
**/
public static LocalDate minusLocalDate(LocalDate localDate, long year, long month, long day) {
localDate = localDate.minusYears(year);
localDate = localDate.minusMonths(month);
localDate = localDate.minusDays(day);
return localDate;
}
/**
* @Description 将要删减的年、月、日、时、分、秒,参数为正数
* @Author duyq
* @Param [localDateTime, year, month, day, hour, minute, second]
* @Date 18:05 2019/7/31
* @return java.time.LocalDateTime
**/
public static LocalDateTime minusLocalDateTime(LocalDateTime localDateTime, long year, long month, long day,
long hour, long minute, long second) {
localDateTime = localDateTime.minusYears(year);
localDateTime = localDateTime.minusMonths(month);
localDateTime = localDateTime.minusDays(day);
localDateTime = localDateTime.minusHours(hour);
localDateTime = localDateTime.minusMinutes(minute);
localDateTime = localDateTime.minusSeconds(second);
return localDateTime;
}
/**
* @Description 计算两个日期间隔的多少年
* @Author duyq
* @param startDateTime 开始日期
* @param endDateTime 结束日期
* @Date 2019/8/1 11:11
* @return long
**/
public static long diffYear(LocalDateTime startDateTime, LocalDateTime endDateTime) {
return ChronoUnit.YEARS.between(startDateTime, endDateTime);
}
/**
* @Description 计算两个日期间隔的多少月
* @Author duyq
* @param startDateTime 开始日期
* @param endDateTime 结束日期
* @Date 2019/8/1 11:11
* @return long
**/
public static long diffMonth(LocalDateTime startDateTime, LocalDateTime endDateTime) {
return ChronoUnit.MONTHS.between(startDateTime, endDateTime);
}
/**
* @Description 计算两个日期间隔的多少天
* @Author duyq
* @param startDateTime 开始日期
* @param endDateTime 结束日期
* @Date 2019/8/1 11:11
* @return long
**/
public static long diffDay(LocalDateTime startDateTime, LocalDateTime endDateTime) {
return ChronoUnit.DAYS.between(startDateTime, endDateTime);
}
/**
* @Description 计算两个日期间隔的多少小时
* @Author duyq
* @param startDateTime 开始日期
* @param endDateTime 结束日期
* @Date 2019/8/1 11:11
* @return long
**/
public static long diffHour(LocalDateTime startDateTime, LocalDateTime endDateTime) {
return ChronoUnit.HOURS.between(startDateTime, endDateTime);
}
/**
* @Description 计算两个日期间隔的多少分钟
* @Author duyq
* @param startDateTime 开始日期
* @param endDateTime 结束日期
* @Date 2019/8/1 11:11
* @return long
**/
public static long diffMinute(LocalDateTime startDateTime, LocalDateTime endDateTime) {
return ChronoUnit.MINUTES.between(startDateTime, endDateTime);
}
/**
* @Description 计算两个日期间隔的多少秒
* @Author duyq
* @param startDateTime 开始日期
* @param endDateTime 结束日期
* @Date 2019/8/1 11:11
* @return long
**/
public static long diffSecond(LocalDateTime startDateTime, LocalDateTime endDateTime) {
return ChronoUnit.SECONDS.between(startDateTime, endDateTime);
}
public static void main(String[] args) {
String date0 = formatLocalDate(YYYY_U_MM_U_DD);
System.out.println(date0);
String s = formatLocalDateTime(YYYY_U_MM_U_DD_HH_U_MM_U_SS_U);
System.out.println(s);
String time = formatLocalTime(HH_U_MM_U_SS_U);
System.out.println(time);
String formatLocalDate = getFormatLocalDate(getLocalDate(), "");
System.out.println(formatLocalDate);
Date date1 = localDateToDate(LocalDate.now());
System.out.println(date1);
Date date2 = localDateTimeToDate(LocalDateTime.now());
System.out.println(date2);
LocalDate localDate = dateToLocalDate(new Date());
System.out.println(localDate);
LocalDateTime localDateTime = dateToLocalDateTime(new Date());
System.out.println(localDateTime);
String beginDateTime = getBeginDateTime(LocalDateTime.now().minusDays(1));
System.out.println(beginDateTime);
String endDateTime = getEndDateTime(LocalDateTime.now().minusDays(1));
System.out.println(endDateTime);
LocalDate date3 = getDate("2018年09月25日", YYYY_U_MM_U_DD);
System.out.println(date3);
LocalDate localDate1 = plusLocalDate(LocalDate.now(), 5, 5, 5);
System.out.println("localDate1 = " + localDate1);
LocalDateTime dateTime = getDateTime("2018-01-01 10:12:05", YYYY_MM_DD_HH_MM_SS);
LocalDateTime now = LocalDateTime.now();
System.out.println(dateTime);
System.out.println(now);
System.out.println("相差年:" + diffYear(dateTime, now));
System.out.println("相差月:" + diffMonth(dateTime, now));
System.out.println("相差日:" + diffDay(dateTime, now));
System.out.println("相差时:" + diffHour(dateTime, now));
System.out.println("相差分:" + diffMinute(dateTime, now));
System.out.println("相差秒:" + diffSecond(dateTime, now));
}
}
java8日期时间处理工具类
最新推荐文章于 2023-10-05 22:06:23 发布