package com. max. common. utils. date ;
import cn. hutool. core. date. DateUtil ;
import org. apache. commons. lang. StringUtils ;
import org. slf4j. Logger ;
import org. slf4j. LoggerFactory ;
import java. text. ParseException ;
import java. text. SimpleDateFormat ;
import java. time. DayOfWeek ;
import java. time. LocalDate ;
import java. time. Month ;
import java. time. format. DateTimeFormatter ;
import java. util. Calendar ;
import java. util. Date ;
private final static Logger logger = LoggerFactory . getLogger ( DateUtils . class ) ;
public final static String DATE_PATTERN = "yyyy-MM-dd" ;
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss" ;
public final static String SUBJECT_DATE = "yyyy/MM/dd" ;
日期格式转换
public static Date format ( String time, String pattern) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( pattern) ;
Date date = simpleDateFormat. parse ( time. trim ( ) ) ;
return date;
}
比较两个日期
public static boolean isBetween ( String startStr, String endStr) throws ParseException {
Date now = new Date ( ) ;
Date start = DateUtils . format ( startStr, DATE_TIME_PATTERN ) ;
Date end = DateUtils . format ( endStr, DATE_TIME_PATTERN ) ;
if ( now. getTime ( ) > start. getTime ( ) && now. getTime ( ) < end. getTime ( ) ) {
return true ;
} else {
return false ;
}
}
当前日期加 一天、一年、一月
public static Date addDays ( Date date, int days) {
Date theDate = org. apache. commons. lang. time. DateUtils. addDays ( date, days) ;
return theDate;
}
public static Date addYear ( Date date, int days) {
Date theDate = org. apache. commons. lang. time. DateUtils. addYears ( date, days) ;
return theDate;
}
public static Date addMonth ( Date date, int days) {
Date theDate = org. apache. commons. lang. time. DateUtils. addMonths ( date, days) ;
return theDate;
}
计算两个时间相差多少个小时
public static Float getHours ( Date startDate, Date endDate) {
if ( startDate == null || endDate == null ) {
return 0F ;
}
long s = startDate. getTime ( ) ;
long e = endDate. getTime ( ) ;
if ( s > e) {
return 0F ;
}
Float hours = ( ( ( float ) ( e - s) ) / ( 1000 * 60 * 60 ) ) ;
return hours;
}
计算距离现在多久,非精确
public static String getTimeBefore ( Date date) {
Date now = new Date ( ) ;
long l = now. getTime ( ) - date. getTime ( ) ;
long day = l / ( 24 * 60 * 60 * 1000 ) ;
long hour = ( l / ( 60 * 60 * 1000 ) - day * 24 ) ;
long min = ( ( l / ( 60 * 1000 ) ) - day * 24 * 60 - hour * 60 ) ;
long s = ( l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60 ) ;
String r = "" ;
if ( day > 0 ) {
r += day + "天" ;
} else if ( hour > 0 ) {
r += hour + "小时" ;
} else if ( min > 0 ) {
r += min + "分" ;
} else if ( s > 0 ) {
r += s + "秒" ;
}
r += "前" ;
return r;
}
计算距离现在多久,精确
public static String getTimeBeforeAccurate ( Date date) {
Date now = new Date ( ) ;
long l = now. getTime ( ) - date. getTime ( ) ;
long day = l / ( 24 * 60 * 60 * 1000 ) ;
long hour = ( l / ( 60 * 60 * 1000 ) - day * 24 ) ;
long min = ( ( l / ( 60 * 1000 ) ) - day * 24 * 60 - hour * 60 ) ;
long s = ( l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60 ) ;
String r = "" ;
if ( day > 0 ) {
r += day + "天" ;
}
if ( hour > 0 ) {
r += hour + "小时" ;
}
if ( min > 0 ) {
r += min + "分" ;
}
if ( s > 0 ) {
r += s + "秒" ;
}
r += "前" ;
return r;
}
string格式的日期转 date
public static Date string2Date ( final String dateStr) {
return string2Date ( dateStr, DATE_PATTERN ) ;
}
public final static String DATE_PATTERN = "yyyy-MM-dd" ;
string格式的日期转 date
public static Date string2Date ( final String dateStr, String format) {
if ( StringUtils . isEmpty ( format) || StringUtils . isBlank ( format) )
return null ;
SimpleDateFormat dateFormat = new SimpleDateFormat ( format) ;
dateFormat. setLenient ( false ) ;
Date date = null ;
try {
date = dateFormat. parse ( dateStr) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
return date;
}
date转 string (yyyy-MM-dd)
public static String format ( Date date) {
return format ( date, DATE_PATTERN ) ;
}
public static String format ( Date date, String pattern) {
if ( date != null ) {
SimpleDateFormat df = new SimpleDateFormat ( pattern) ;
return df. format ( date) ;
}
return null ;
}
public static String date2String ( final Date date) {
return date2String ( date, DATE_PATTERN ) ;
}
public final static String DATE_PATTERN = "yyyy-MM-dd" ;
public static String date2String ( final Date date, String format) {
if ( date == null )
return null ;
if ( StringUtils . isEmpty ( format) || StringUtils . isBlank ( format) )
format = DATE_PATTERN ;
SimpleDateFormat dateFormat = new SimpleDateFormat ( format) ;
dateFormat. setLenient ( false ) ;
return dateFormat. format ( date) ;
}
public final static String DATE_PATTERN = "yyyy-MM-dd" ;
获取当前时间前一小时的时间
public static Date beforeOneHourToNowDate ( Date date) {
Calendar calendar = Calendar . getInstance ( ) ;
calendar. setTime ( date) ;
calendar. add ( Calendar . HOUR_OF_DAY , - 1 ) ;
return calendar. getTime ( ) ;
}
获取天 第一天或最后一天
public static String getStartOrEndDayOfDay ( String today, long weeks, Boolean isFirst) {
String min = " 00:00:00" ;
String max = " 59:59:59" ;
LocalDate time = LocalDate . parse ( today, DateTimeFormatter . ofPattern ( "yyyy-MM-dd" ) ) ;
LocalDate resDate = time. plusDays ( weeks) ;
String date;
if ( isFirst) {
date = LocalDate . of ( resDate. getYear ( ) , resDate. getMonth ( ) , resDate. getDayOfMonth ( ) ) + min;
} else {
date = LocalDate . of ( resDate. getYear ( ) , resDate. getMonth ( ) , resDate. getDayOfMonth ( ) ) + max;
}
return date;
}
获取周 第一天或最后一天
public static String getStartOrEndDayOfWeek ( String today, long weeks, Boolean isFirst) {
String min = " 00:00:00" ;
String max = " 59:59:59" ;
LocalDate time = LocalDate . parse ( today, DateTimeFormatter . ofPattern ( "yyyy-MM-dd" ) ) ;
LocalDate resDate = time. plusWeeks ( weeks) ;
DayOfWeek week = resDate. getDayOfWeek ( ) ;
int value = week. getValue ( ) ;
String date;
if ( isFirst) {
date = resDate. minusDays ( value - 1 ) + min;
} else {
date = resDate. plusDays ( 7 - value) + max;
}
return date;
}
获取月份 第一天或最后一天
public static String getStartOrEndDayOfMonth ( String today, long months, Boolean isFirst) {
String min = " 00:00:00" ;
String max = " 59:59:59" ;
LocalDate time = LocalDate . parse ( today, DateTimeFormatter . ofPattern ( "yyyy-MM-dd" ) ) ;
LocalDate resDate = time. plusMonths ( months) ;
Month month = resDate. getMonth ( ) ;
int length = month. length ( resDate. isLeapYear ( ) ) ;
String date;
if ( isFirst) {
date = LocalDate . of ( resDate. getYear ( ) , month, 1 ) + min;
} else {
date = LocalDate . of ( resDate. getYear ( ) , month, length) + max;
}
return date;
}
获取季度 第一天或最后一天
public static String getStartOrEndDayOfQuarter ( String today, long quarters, Boolean isFirst) {
String min = " 00:00:00" ;
String max = " 59:59:59" ;
LocalDate time = LocalDate . parse ( today, DateTimeFormatter . ofPattern ( "yyyy-MM-dd" ) ) ;
LocalDate resDate = time. plusMonths ( quarters * 3 ) ;
Month month = resDate. getMonth ( ) ;
Month firstMonthOfQuarter = month. firstMonthOfQuarter ( ) ;
Month endMonthOfQuarter = Month . of ( firstMonthOfQuarter. getValue ( ) + 2 ) ;
String date;
if ( isFirst) {
date = LocalDate . of ( resDate. getYear ( ) , firstMonthOfQuarter, 1 ) + min;
} else {
date = LocalDate . of ( resDate. getYear ( ) , endMonthOfQuarter, endMonthOfQuarter. length ( resDate. isLeapYear ( ) ) ) + max;
}
return date;
}
获取年份 第一天或最后一天
public static String getStartOrEndDayOfYear ( String date, long type, Boolean isFlag) {
String min = " 00:00:00" ;
String max = " 59:59:59" ;
LocalDate time = LocalDate . parse ( date, DateTimeFormatter . ofPattern ( "yyyy-MM-dd" ) ) ;
LocalDate resDate = time. plusYears ( type) ;
String finalDate;
if ( isFlag) {
finalDate = LocalDate . of ( resDate. getYear ( ) , Month . JANUARY , 1 ) + " 00:00:00" ;
} else {
finalDate = LocalDate . of ( resDate. getYear ( ) , Month . DECEMBER , Month . DECEMBER . length ( resDate. isLeapYear ( ) ) ) + " 59:59:59" ;
}
return finalDate;
}