Date
package com. qiangesoft. utils. date ; import java. text. SimpleDateFormat ;
import java. util. Calendar ;
import java. util. Date ;
public class DateUtil { public static final String [ ] CHINESE_WEEK_DAY = new String [ ] { "周日" , "周一" , "周二" , "周三" , "周四" , "周五" , "周六" } ; public static Date now ( ) { return new Date ( ) ; } public static Date startOfDay ( ) { return startOfDay ( now ( ) ) ; } public static Date startOfDay ( Date date) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; 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 ( ) ; } public static Date endOfDay ( ) { return endOfDay ( now ( ) ) ; } public static Date endOfDay ( Date date) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. set ( Calendar . HOUR_OF_DAY , 23 ) ; calendar. set ( Calendar . MINUTE , 59 ) ; calendar. set ( Calendar . SECOND , 59 ) ; calendar. set ( Calendar . MILLISECOND , 999 ) ; return calendar. getTime ( ) ; } public static int dayOfWeek ( ) { return dayOfWeek ( now ( ) ) ; } public static int dayOfWeek ( Date date) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; return calendar. get ( Calendar . DAY_OF_WEEK ) ; } public static Date startOfMonth ( ) { return startOfMonth ( now ( ) ) ; } public static Date startOfMonth ( Date date) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. set ( Calendar . DAY_OF_MONTH , 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 ( ) ; } public static Date endOfMonth ( ) { return endOfMonth ( now ( ) ) ; } public static Date endOfMonth ( Date date) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. set ( Calendar . DAY_OF_MONTH , calendar. getActualMaximum ( Calendar . DAY_OF_MONTH ) ) ; calendar. set ( Calendar . HOUR_OF_DAY , 23 ) ; calendar. set ( Calendar . MINUTE , 59 ) ; calendar. set ( Calendar . SECOND , 59 ) ; calendar. set ( Calendar . MILLISECOND , 999 ) ; return calendar. getTime ( ) ; } public static Boolean between ( Date startTime, Date endTime) { return between ( now ( ) , startTime, endTime) ; } public static Boolean between ( Date date, Date startTime, Date endTime) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; Calendar start = Calendar . getInstance ( ) ; start. setTime ( startTime) ; Calendar end = Calendar . getInstance ( ) ; end. setTime ( endTime) ; return calendar. after ( start) && calendar. before ( end) ; } public static String format ( Date date, String format) { SimpleDateFormat dateFormat = new SimpleDateFormat ( format) ; return dateFormat. format ( date) ; } public static Date addSecond ( Date date, int second) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. add ( Calendar . SECOND , second) ; return calendar. getTime ( ) ; } public static Date addMinute ( Date date, int minute) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. add ( Calendar . MINUTE , minute) ; return calendar. getTime ( ) ; } public static Date addHour ( Date date, int hour) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. add ( Calendar . HOUR , hour) ; return calendar. getTime ( ) ; } public static Date addDay ( Date date, int day) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. add ( Calendar . DATE , day) ; return calendar. getTime ( ) ; } public static Date addWeek ( Date date, int week) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. add ( Calendar . DAY_OF_WEEK , week) ; return calendar. getTime ( ) ; } public static Date addMonth ( Date date, int month) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. add ( Calendar . MONTH , month) ; return calendar. getTime ( ) ; } public static Date addYear ( Date date, int year) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. add ( Calendar . YEAR , year) ; return calendar. getTime ( ) ; } public static Date reduceSecond ( Date date, int second) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. roll ( Calendar . SECOND , second) ; return calendar. getTime ( ) ; } public static Date reduceMinute ( Date date, int minute) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. roll ( Calendar . MINUTE , minute) ; return calendar. getTime ( ) ; } public static Date reduceHour ( Date date, int hour) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. roll ( Calendar . HOUR , hour) ; return calendar. getTime ( ) ; } public static Date reduceDay ( Date date, int day) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. roll ( Calendar . DATE , day) ; return calendar. getTime ( ) ; } public static Date reduceWeek ( Date date, int week) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. roll ( Calendar . DAY_OF_WEEK , week) ; return calendar. getTime ( ) ; } public static Date reduceMonth ( Date date, int month) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. roll ( Calendar . MONTH , month) ; return calendar. getTime ( ) ; } public static Date reduceYear ( Date date, int year) { Calendar calendar = Calendar . getInstance ( ) ; calendar. setTime ( date) ; calendar. roll ( Calendar . YEAR , year) ; return calendar. getTime ( ) ; } }
LocalDateTime
package com. qiangesoft. utils. date ; import java. time. DayOfWeek ;
import java. time. LocalDateTime ;
import java. time. LocalTime ;
import java. time. format. DateTimeFormatter ;
import java. time. temporal. TemporalAdjusters ;
public class LocalDateUtil { public static final String [ ] CHINESE_WEEK_DAY = new String [ ] { "周日" , "周一" , "周二" , "周三" , "周四" , "周五" , "周六" } ; public static LocalDateTime now ( ) { return LocalDateTime . now ( ) ; } public static LocalDateTime startOfDay ( ) { return now ( ) . toLocalDate ( ) . atStartOfDay ( ) ; } public static LocalDateTime startOfDay ( LocalDateTime date) { return date. toLocalDate ( ) . atStartOfDay ( ) ; } public static LocalDateTime endOfDay ( ) { return now ( ) . toLocalDate ( ) . atTime ( LocalTime . MAX ) ; } public static LocalDateTime endOfDay ( LocalDateTime date) { return date. toLocalDate ( ) . atTime ( LocalTime . MAX ) ; } public static int dayOfWeek ( ) { return dayOfWeek ( now ( ) ) ; } public static int dayOfWeek ( LocalDateTime date) { DayOfWeek dayOfWeek = date. getDayOfWeek ( ) ; return dayOfWeek. getValue ( ) ; } public static LocalDateTime startOfMonth ( ) { return startOfMonth ( now ( ) ) ; } public static LocalDateTime startOfMonth ( LocalDateTime date) { return date. toLocalDate ( ) . withDayOfMonth ( 1 ) . atStartOfDay ( ) ; } public static LocalDateTime endOfMonth ( ) { return endOfMonth ( now ( ) ) ; } public static LocalDateTime endOfMonth ( LocalDateTime date) { return date. toLocalDate ( ) . with ( TemporalAdjusters . lastDayOfMonth ( ) ) . atTime ( LocalTime . MAX ) ; } public static Boolean between ( LocalDateTime startTime, LocalDateTime endTime) { return between ( now ( ) , startTime, endTime) ; } public static Boolean between ( LocalDateTime date, LocalDateTime startTime, LocalDateTime endTime) { return date. isAfter ( startTime) && date. isBefore ( endTime) ; } public static String format ( LocalDateTime date, String format) { DateTimeFormatter formatter = DateTimeFormatter . ofPattern ( format) ; return date. format ( formatter) ; } public static LocalDateTime addSecond ( LocalDateTime date, int second) { return date. plusSeconds ( second) ; } public static LocalDateTime addMinute ( LocalDateTime date, int minute) { return date. plusMinutes ( minute) ; } public static LocalDateTime addHour ( LocalDateTime date, int hour) { return date. plusHours ( hour) ; } public static LocalDateTime addDay ( LocalDateTime date, int day) { return date. plusDays ( day) ; } public static LocalDateTime addWeek ( LocalDateTime date, int week) { return date. plusWeeks ( week) ; } public static LocalDateTime addMonth ( LocalDateTime date, int month) { return date. plusMonths ( month) ; } public static LocalDateTime addYear ( LocalDateTime date, int year) { return date. plusYears ( year) ; } public static LocalDateTime reduceSecond ( LocalDateTime date, int second) { return date. minusSeconds ( second) ; } public static LocalDateTime reduceMinute ( LocalDateTime date, int minute) { return date. minusMinutes ( minute) ; } public static LocalDateTime reduceHour ( LocalDateTime date, int hour) { return date. minusHours ( hour) ; } public static LocalDateTime reduceDay ( LocalDateTime date, int day) { return date. minusDays ( day) ; } public static LocalDateTime reduceWeek ( LocalDateTime date, int week) { return date. minusWeeks ( week) ; } public static LocalDateTime reduceMonth ( LocalDateTime date, int month) { return date. minusMonths ( month) ; } public static LocalDateTime reduceYear ( LocalDateTime date, int year) { return date. minusYears ( year) ; } }