You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
6.4 KiB
Java
183 lines
6.4 KiB
Java
package com.engine.common.util;
|
|
|
|
import java.time.Duration;
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDateTime;
|
|
import java.time.Period;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
public class DateUtil {
|
|
public static DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
public static DateTimeFormatter yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
public static DateTimeFormatter yyyyMMddHHmm = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
|
public static DateTimeFormatter yyyyMM = DateTimeFormatter.ofPattern("yyyy-MM");
|
|
|
|
public static String beforeMonth(String time){
|
|
LocalDateTime localDateTime = LocalDate.parse(time, yyyyMMdd).atStartOfDay();
|
|
return localDateTime.minusMonths(1).format(yyyyMM);
|
|
}
|
|
public static String lastMonth(String time){
|
|
LocalDateTime localDateTime = LocalDate.parse(time, yyyyMMdd).atStartOfDay();
|
|
|
|
return localDateTime.plusMonths(1).format(yyyyMM);
|
|
}
|
|
|
|
|
|
public static String beforeDay(String time,long day){
|
|
LocalDateTime localDateTime = LocalDate.parse(time, yyyyMMdd).atStartOfDay();
|
|
return localDateTime.minusDays(day).format(yyyyMMdd);
|
|
}
|
|
public static String AfterDay(String time,long day){
|
|
LocalDateTime localDateTime = LocalDate.parse(time, yyyyMMdd).atStartOfDay();
|
|
|
|
return localDateTime.plusDays(day).format(yyyyMMdd);
|
|
}
|
|
|
|
public static String beforeMinutes(String time,long minutes){
|
|
LocalDateTime localDateTime = DateUtil.getTime(time);
|
|
return localDateTime.minusDays(minutes).format(yyyyMMddHHmm);
|
|
}
|
|
|
|
public static String AfterMinutes(String time,long minutes){
|
|
LocalDateTime localDateTime = DateUtil.getTime(time);
|
|
return localDateTime.plusMinutes(minutes).format(yyyyMMddHHmm);
|
|
}
|
|
|
|
public static String nowMonth(String time){
|
|
LocalDateTime localDateTime = LocalDate.parse(time, yyyyMMdd).atStartOfDay();
|
|
return localDateTime.format(yyyyMM);
|
|
}
|
|
|
|
public static String getCurrentDate(){
|
|
return LocalDateTime.now().format(yyyyMMdd);
|
|
}
|
|
public static String getCurrentTime(){
|
|
return LocalDateTime.now().format(yyyyMMddHHmmss);
|
|
}
|
|
public static String getCurrentTime(String timeType){
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(timeType);
|
|
return LocalDateTime.now().format(formatter);
|
|
}
|
|
|
|
//获得往后日期
|
|
public static String getCurrentDatePlusDay(int day){
|
|
return LocalDateTime.now().plusDays(day).format(yyyyMMdd);
|
|
}
|
|
//获得往前日期
|
|
public static String getCurrentDateMinusDay(int day){
|
|
return LocalDateTime.now().minusDays(day).format(yyyyMMdd);
|
|
}
|
|
|
|
|
|
public static LocalDateTime getTime(String time){
|
|
int length = time.length();
|
|
switch (length){
|
|
case 7:
|
|
return LocalDateTime.parse(time, yyyyMM);
|
|
case 10:
|
|
return LocalDateTime.parse(time, yyyyMMdd);
|
|
case 19:
|
|
return LocalDateTime.parse(time, yyyyMMddHHmmss);
|
|
case 16:
|
|
return LocalDateTime.parse(time, yyyyMMddHHmm);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获得时间与当前相差的小时
|
|
* @param startTime 开始时间 yyyy-MM-dd HH:mm:ss
|
|
* @return
|
|
*/
|
|
public static long getBetWeenHours(String startTime){
|
|
|
|
Duration duration = Duration.between(LocalDateTime.parse(startTime,yyyyMMddHHmmss),LocalDateTime.now());
|
|
return duration.toHours();
|
|
}
|
|
/**
|
|
* 获得两个时间相差的年份
|
|
* @param startTime 开始时间 yyyy-MM-dd
|
|
* @return
|
|
*/
|
|
public static int getBetWeenYears(String startTime){
|
|
Period duration = Period.between(LocalDate.parse(startTime,yyyyMMdd),LocalDateTime.now().toLocalDate());
|
|
return duration.getYears();
|
|
}
|
|
|
|
/**
|
|
* 获得两个时间相差的年份
|
|
* @param startTime 开始时间 yyyy-MM-dd
|
|
* @return
|
|
*/
|
|
public static int getBetWeenYears(String startTime,String endTime){
|
|
Period duration = Period.between(LocalDate.parse(startTime,yyyyMMdd),LocalDate.parse(endTime,yyyyMMdd));
|
|
|
|
return duration.getYears();
|
|
}
|
|
|
|
/**
|
|
* 获得当前时间相差的天数
|
|
* @param startTime 开始时间 yyyy-MM-dd
|
|
* @return
|
|
*/
|
|
public static int getBetWeenDays(String startTime){
|
|
LocalDate startDate = LocalDate.parse(startTime);
|
|
LocalDate endDate = LocalDateTime.now().toLocalDate();
|
|
|
|
long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
|
|
return Math.toIntExact(daysBetween);
|
|
}
|
|
/**
|
|
* 获得当前时间相差的天数
|
|
* @param startTime 开始时间 yyyy-MM-dd
|
|
* @return
|
|
*/
|
|
public static int getBetWeenDays(String startTime,String endTime){
|
|
LocalDate startDate = LocalDate.parse(startTime);
|
|
LocalDate endDate = LocalDate.parse(endTime);
|
|
|
|
long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
|
|
return Math.toIntExact(daysBetween);
|
|
}
|
|
/**
|
|
* 获得当前时间相差的分钟数
|
|
* @param startTime 开始时间 yyyy-MM-dd
|
|
* @return
|
|
*/
|
|
public static int getBetWeenMinutes(String startTime,String endTime,DateTimeFormatter formatter){
|
|
LocalDateTime startDate = LocalDateTime.parse(startTime,formatter);
|
|
LocalDateTime endDate = LocalDateTime.parse(endTime,formatter);
|
|
|
|
long daysBetween = ChronoUnit.MINUTES.between(startDate, endDate);
|
|
return Math.toIntExact(daysBetween);
|
|
}
|
|
/**
|
|
* 获得当前时间相差的分钟数
|
|
* @param startTime 开始时间 yyyy-MM-dd
|
|
* @return
|
|
*/
|
|
public static int getBetWeenMinutes(String startTime,String endTime){
|
|
LocalDateTime startDate = DateUtil.getTime(startTime);
|
|
LocalDateTime endDate = DateUtil.getTime(endTime);
|
|
|
|
long daysBetween = ChronoUnit.MINUTES.between(startDate, endDate);
|
|
return Math.toIntExact(daysBetween);
|
|
}
|
|
|
|
/**
|
|
* 获得两个时间相差的小时
|
|
* @param startTime 开始时间 yyyy-MM-dd HH:mm:ss
|
|
* @param endTime 结束时间 yyyy-MM-dd HH:mm:ss
|
|
* @return
|
|
*/
|
|
public static long getBetWeenHours(String startTime,String endTime){
|
|
Duration duration = Duration.between(LocalDateTime.parse(startTime,yyyyMMddHHmmss),LocalDateTime.parse(endTime,yyyyMMddHHmmss));
|
|
|
|
return duration.toHours();
|
|
}
|
|
|
|
|
|
}
|