依赖
后端接收
@ApiModelProperty(value = "开始时间")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime startTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
时间
package com.lhj.finance.commons.util;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
@Slf4j
public class DateUtils {
public static final String MID_LINE_ALL = "yyyy-MM-dd HH:mm:ss";
public static final String NO_LINE_ALL = "yyyyMMddHHmmss";
public static final String MID_LINE_YMD = "yyyy-MM-dd";
public static final String NO_LINE_YMD = "yyyyMMdd";
public static final String COLON_HMS = "HH:mm:ss";
public static final String NO_COLON_HMS = "HHmmss";
public static String format(Date date, String format){
return new SimpleDateFormat(format).format(date);
}
public static Date parse(String dateStr, String format) {
Date parse = null;
try {
parse = new SimpleDateFormat(format).parse(dateStr);
} catch (ParseException e) {
log.error("日期格式转换异常,{}", e.getMessage());
}
return parse;
}
public static String utcToGst(String utc, String format) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
ZonedDateTime zdt = ZonedDateTime.parse(utc);
LocalDateTime localDateTime = zdt.toLocalDateTime().plusHours(8);
return dtf.format(localDateTime);
}
public static String gstToUtc(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+0"));
return sdf.format(date);
}
public static Date addMinusYearMonthDay(Date date, int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, year);
cal.add(Calendar.MONTH, month);
cal.add(Calendar.DATE, day);
return cal.getTime();
}
public static Date addMinusHourMonthSecond(Date date, int hour, int minute, int second) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, hour);
cal.add(Calendar.MINUTE, minute);
cal.add(Calendar.SECOND, second);
return cal.getTime();
}
public static Date addMinusWeek(Date date, int week) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_WEEK, week);
return cal.getTime();
}
public static List<Integer> timeAllDiff(Date start, Date end) {
List<Integer> ymdList = yearMonthDayDiff(start, end);
ymdList.addAll(hourMinuteSecondDiff(start, end));
return ymdList;
}
public static List<Integer> yearMonthDayDiff(Date start, Date end) {
List<Date> dates = compareExchangeDate(start, end);
Period period = Period.between(toLocalDate(dates.get(0)), toLocalDate(dates.get(1)));
List<Integer> ymdList = new ArrayList<>();
ymdList.add(period.getYears());
ymdList.add(period.getMonths());
ymdList.add(period.getDays());
return ymdList;
}
public static List<Integer> monthDayDiff(Date start, Date end) {
List<Date> dates = compareExchangeDate(start, end);
Period period = Period.between(toLocalDate(dates.get(0)), toLocalDate(dates.get(1)));
List<Integer> ymdList = new ArrayList<>();
ymdList.add(period.getYears() * 12 + period.getMonths());
ymdList.add(period.getDays());
return ymdList;
}
public static List<Integer> dayHourMinuteSecondDiff(Date start, Date end) {
long second = millisecondDiff(start, end) / 1000;
int dd = (int) second / (60 * 60 * 24);
int hh = (int) (second - dd * 60 * 60 * 24) / (60 * 60);
int mm = (int) (second - dd * 60 * 60 * 24 - hh * 60 * 60) / (60);
int ss = (int) (second - dd * 60 * 60 * 24 - hh * 60 * 60 - mm * 60);
List<Integer> ymdList = new ArrayList<>();
ymdList.add(dd);
ymdList.add(hh);
ymdList.add(mm);
ymdList.add(ss);
return ymdList;
}
public static BigDecimal dayDiff(Date start, Date end, int num) {
return quotient(millisecondDiff(start, end), (1000 * 60 * 60 * 24), num);
}
public static List<Integer> hourMinuteSecondDiff(Date start, Date end) {
List<Integer> ymdList = dayHourMinuteSecondDiff(start, end);
ymdList.remove(0);
return ymdList;
}
public static BigDecimal hourDiff(Date start, Date end, int num) {
return quotient(millisecondDiff(start, end), (1000 * 60 * 60), num);
}
public static BigDecimal minuteDiff(Date start, Date end, int num) {
return quotient(millisecondDiff(start, end), (1000 * 60), num);
}
public static BigDecimal secondDiff(Date start, Date end, int num) {
return quotient(millisecondDiff(start, end), (1000), num);
}
public static Long millisecondDiff(Date start, Date end) {
List<Date> dates = compareExchangeDate(start, end);
return dates.get(1).getTime() - dates.get(0).getTime();
}
public static boolean between(Date startDate, Date endDate, Date date) {
List<Date> dates = compareExchangeDate(startDate, endDate);
return date.after(dates.get(0)) && date.before(dates.get(1));
}
public static BigDecimal quotient(long dividend, long divisor, int num) {
if(num>0){
return BigDecimal.valueOf(dividend).divide(BigDecimal.valueOf(divisor), num, BigDecimal.ROUND_HALF_UP);
}
return BigDecimal.valueOf(dividend).divideToIntegralValue(BigDecimal.valueOf(divisor));
}
public static List<Date> compareExchangeDate(Date start, Date end){
if (end.compareTo(start)<0) {
Date tmp = end;
end = start;
start = tmp;
}
List<Date> dates = new ArrayList<>();
dates.add(start);
dates.add(end);
return dates;
}
public static String normalOrLeapYear(Date date) {
String yearStr = "normal";
LocalDate localDate = toLocalDate(date);
int year = localDate.getYear();
int a = year % 4;
int b = year % 100;
int c = year % 400;
if(a==0 && b!=0 || c==0) {
yearStr = "leap";
}
return yearStr;
}
public static Date toDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
}
public static Date toDateTime(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
public static LocalDate toLocalDate(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
}
public static LocalDateTime toLocalDateTime(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
}
public static Long dateToTimestamp(Date date){
return date.getTime();
}
public static Date timestampToDate(Long timestamp){
return new Date(timestamp);
}
public static Long timeDiff(Long startTimestamp){
return System.currentTimeMillis() - startTimestamp;
}
public static Long timeDiff(StopWatch stopWatch){
stopWatch.stop();
return stopWatch.getTotalTimeMillis();
}
public static Date strToDate(String oirStr){
return new Date(oirStr);
}
}