package com.zy.common.utils;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.Objects;
|
import java.util.concurrent.TimeUnit;
|
|
public class DateUtils {
|
public static final String yyyyMMdd_C = "yyyy年MM月dd日";
|
public static final String yyyyMM_F = "yyyy-MM";
|
public static final String yyyyMMdd_F = "yyyy-MM-dd";
|
public static final String yyyyMMddHHmmss_F = "yyyy-MM-dd HH:mm:ss";
|
public static final String yyyyMMddHHmmsssss_F = "yyyy-MM-dd HH:mm:ss,SSS";
|
public static final String yyyy = "yyyy";
|
public static final String yyyyMM = "yyyyMM";
|
public static final String yyyyMMdd = "yyyyMMdd";
|
public static final String yyyyMMddHH = "yyyyMMddHH";
|
public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
|
public static final String YYMMDDHHMMSS = "YYMMDDHHMMSS";
|
public static final String yyyyMMddHHmmsssss = "yyyyMMddHHmmssSSS";
|
|
public static String convert(Date date, String pattern) {
|
return (new SimpleDateFormat(pattern)).format(date);
|
}
|
|
public static String convert(Date date) {
|
return convert(date, "yyyy-MM-dd HH:mm:ss");
|
}
|
|
public static Date convert(String str, String pattern) {
|
if (str.length() < pattern.length()) {
|
throw new RuntimeException("时间解析失败 ==>> " + str);
|
} else {
|
if (str.length() > pattern.length()) {
|
str = str.substring(0, pattern.length());
|
}
|
|
SimpleDateFormat format = new SimpleDateFormat(pattern);
|
|
try {
|
Date date = format.parse(str);
|
return date;
|
} catch (ParseException var5) {
|
throw new RuntimeException("时间解析失败 ==>> " + str);
|
}
|
}
|
}
|
|
public static Date convert(String str) {
|
return convert(str, "yyyy-MM-dd HH:mm:ss");
|
}
|
|
public static int diff(Date date1, Date date2) {
|
return getDaysByTimestamp(Math.abs(date2.getTime() - date1.getTime()));
|
}
|
|
public static long diffToMinute(Date date1, Date date2) {
|
return Math.abs(date2.getTime() - date1.getTime()) / 1000L / 60L;
|
}
|
|
public static long diffToSeconds(Date date1, Date date2) {
|
return Math.abs(date2.getTime() - date1.getTime()) / 1000L;
|
}
|
|
private static int getDaysByTimestamp(long timestamp) {
|
double daysPoint = Arith.divides(2, new Number[]{timestamp, 86400000});
|
int daysPoint1 = (int)daysPoint;
|
double daysPoint2 = (double)daysPoint1;
|
return daysPoint > daysPoint2 ? daysPoint1 + 1 : daysPoint1;
|
}
|
|
public static int diffToNow(Date date) {
|
long diff = (new Date()).getTime() - date.getTime();
|
return (int)(Math.abs(diff) / 1000L);
|
}
|
|
public static String createTimeStamp() {
|
return Long.toString(System.currentTimeMillis() / 1000L);
|
}
|
|
public static Date calculate(Date date, Long val, TimeUnit timeUnit, boolean subtraction) {
|
return !Objects.isNull(date) && !Objects.isNull(val) && !Objects.isNull(timeUnit) ? new Date(subtraction ? date.getTime() - timeUnit.toMillis(val) : date.getTime() + timeUnit.toMillis(val)) : null;
|
}
|
|
public static Date calculate(Date date, Long val, TimeUnit timeUnit) {
|
return calculate(date, val, timeUnit, false);
|
}
|
|
public static DateEntity getDateEntity(Date date) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
DateEntity dateEntity = new DateEntity();
|
dateEntity.setYear(cal.get(1));
|
dateEntity.setMonth(cal.get(2));
|
dateEntity.setDay(cal.get(5));
|
dateEntity.setHour(cal.get(11));
|
dateEntity.setMinute(cal.get(12));
|
dateEntity.setSecond(cal.get(13));
|
return dateEntity;
|
}
|
|
static class DateEntity {
|
int year;
|
int month;
|
int day;
|
int hour;
|
int minute;
|
int second;
|
|
public int getYear() {
|
return this.year;
|
}
|
|
public void setYear(int year) {
|
this.year = year;
|
}
|
|
public int getMonth() {
|
return this.month;
|
}
|
|
public void setMonth(int month) {
|
this.month = month + 1;
|
}
|
|
public int getDay() {
|
return this.day;
|
}
|
|
public void setDay(int day) {
|
this.day = day;
|
}
|
|
public int getHour() {
|
return this.hour;
|
}
|
|
public void setHour(int hour) {
|
this.hour = hour;
|
}
|
|
public int getMinute() {
|
return this.minute;
|
}
|
|
public void setMinute(int minute) {
|
this.minute = minute;
|
}
|
|
public int getSecond() {
|
return this.second;
|
}
|
|
public void setSecond(int second) {
|
this.second = second;
|
}
|
}
|
}
|