package com.xz.zhaolan.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String TIME_FORMAT = "HH:mm:ss";
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static String getFormatDate(Date date) {
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
return format.format(date);
}
public static String getFormatTime(Date date) {
SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT);
return format.format(date);
}
public static String getFormatDateTime(Date date) {
SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
return format.format(date);
}
public static String getFormatDate(LocalDate localDate) {
return localDate.format(DATE_FORMATTER);
}
public static String getFormatTime(LocalDateTime localDateTime) {
return localDateTime.format(TIME_FORMATTER);
}
public static String getFormatDateTime(LocalDateTime localDateTime) {
return localDateTime.format(DATE_TIME_FORMATTER);
}
public static LocalDateTime dateStrToLocalDateTime(String dateStr) {
return LocalDateTime.parse(dateStr, DATE_TIME_FORMATTER);
}
public static LocalDate dateStrToLocalDate(String dateStr) {
return LocalDate.parse(dateStr, DATE_FORMATTER);
}
public static Date dateStrToDate(String dateStr) {
return Date.from(LocalDate.parse(dateStr, DATE_FORMATTER).atStartOfDay(ZoneId.systemDefault()).toInstant());
}
public static Date dateTimeStrToDate(String dateStr) {
return Date.from(LocalDateTime.parse(dateStr, DATE_TIME_FORMATTER).atZone(ZoneId.systemDefault()).toInstant());
}
public static Date localDateToDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
public static LocalDate dateToLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
public static LocalDateTime dateToLocalDateTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
public static long dateDifference(String start, String end) {
SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
try {
Date startTime = format.parse(start);
Date endTime = format.parse(end);
return endTime.getTime() - startTime.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
public static long calculationDays(String start, String end) {
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date fromDate = simpleFormat.parse(start);
Date toDate = simpleFormat.parse(end);
long from = fromDate.getTime();
long to = toDate.getTime();
long days = (int) ((to - from) / (1000 * 60 * 60 * 24));
return days;
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
public static long calculationHours(String start, String end) {
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date fromDate = simpleFormat.parse(start);
Date toDate = simpleFormat.parse(end);
long from = fromDate.getTime();
long to = toDate.getTime();
long hours = (int) ((to - from) / (1000 * 60 * 60));
return hours;
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
public static long calculationMinutes(String start, String end) {
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date fromDate = simpleFormat.parse(start);
Date toDate = simpleFormat.parse(end);
long from = fromDate.getTime();
long to = toDate.getTime();
long minutes = (int) ((to - from) / (1000 * 60));
return minutes;
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
public static long calculationSecond(String start, String end) {
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date fromDate = simpleFormat.parse(start);
Date toDate = simpleFormat.parse(end);
long from = fromDate.getTime();
long to = toDate.getTime();
long seconds = (int) ((to - from) / 1000);
return seconds;
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
if (field == ChronoUnit.YEARS){
return period.getYears();
}
if (field == ChronoUnit.MONTHS) {
return period.getYears() * 12 + period.getMonths();
}
return field.between(startTime, endTime);
}
public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
return time.plus(number, field);
}
public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){
return time.minus(number,field);
}
public static Date calculationDate(Date date, int field, int number) {
Calendar calendar = Calendar.getInstance();
calendar.add(field, number);
return calendar.getTime();
}
public static boolean compareDate(Date firstDate, Date secondDate) {
return firstDate.getTime() < secondDate.getTime();
}
public static String changeSecond(long second) {
int seconds = Integer.parseInt(String.valueOf(second));
int temp=0;
StringBuffer sb=new StringBuffer();
temp = seconds/3600;
sb.append((temp<10)?"0"+temp+":":""+temp+":");
temp=seconds%3600/60;
sb.append((temp<10)?"0"+temp+":":""+temp+":");
temp=seconds%3600%60;
sb.append((temp<10)?"0"+temp:""+temp);
return sb.toString();
}
public boolean localDateIsBefore(LocalDate firstDate, LocalDate secondDate) {
return firstDate.isBefore(secondDate);
}
public static void main(String[] args) {
Date date = new Date();
LocalDateTime localDate = DateUtils.dateToLocalDateTime(date);
System.out.println(localDate);
String start = "2020-03-01 12:00:00";
String end = "2022-03-12 12:01:00";
long seconds = DateUtils.calculationSecond(start, end);
System.out.println("相差秒数: " + seconds);
long minutes = DateUtils.calculationMinutes(start, end);
System.out.println("相差分钟数: " + minutes);
long days = DateUtils.calculationDays(start, end);
System.out.println("相差天数: " + days);
LocalDateTime startTime = LocalDateTime.of(2000, 10, 13, 11, 11);
LocalDateTime endTime = LocalDateTime.of(2000, 10, 14, 13, 13);
long time = DateUtils.betweenTwoTime(startTime, endTime, ChronoUnit.SECONDS);
System.out.println(time);
date = DateUtils.calculationDate(date, Calendar.DATE, -10);
System.out.println("减去10天: " + DateUtils.getFormatDate(date));
}
}