package com.core.common;
|
|
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 format) {
|
return new SimpleDateFormat(format).format(date);
|
}
|
|
public static String convert(Date date) {
|
return convert(date, yyyyMMddHHmmss_F);
|
}
|
|
public static Date convert(String text, String format) {
|
if (text.length() < format.length()) {
|
throw new RuntimeException("时间解析失败 ==>>" + text);
|
}
|
if (text.length() > format.length()) {
|
text = text.substring(0, format.length());
|
}
|
try {
|
return new SimpleDateFormat(format).parse(text);
|
} catch (ParseException e) {
|
throw new RuntimeException("时间解析失败 ==>>" + text);
|
}
|
}
|
|
public static Date convert(String text) {
|
return convert(text, yyyyMMddHHmmss_F);
|
}
|
|
public static int diff(Date start, Date end) {
|
return getDaysByTimestamp(Math.abs(end.getTime() - start.getTime()));
|
}
|
|
public static long diffToMinute(Date start, Date end) {
|
return Math.abs(end.getTime() - start.getTime()) / 1000L / 60L;
|
}
|
|
public static long diffToSeconds(Date start, Date end) {
|
return Math.abs(end.getTime() - start.getTime()) / 1000L;
|
}
|
|
private static int getDaysByTimestamp(long timestamp) {
|
double days = Arith.divides(2, timestamp, 86400000);
|
int value = (int) days;
|
if (days > value) {
|
return value + 1;
|
}
|
return value;
|
}
|
|
public static int diffToNow(Date date) {
|
long val = new Date().getTime() - date.getTime();
|
return (int) (Math.abs(val) / 1000L);
|
}
|
|
public static String createTimeStamp() {
|
return Long.toString(System.currentTimeMillis() / 1000L);
|
}
|
|
public static Date calculate(Date date, Long amount, TimeUnit unit, boolean before) {
|
if (Objects.isNull(date) || Objects.isNull(amount) || Objects.isNull(unit)) {
|
return null;
|
}
|
return new Date(before ? date.getTime() - unit.toMillis(amount) : date.getTime() + unit.toMillis(amount));
|
}
|
|
public static Date calculate(Date date, Long amount, TimeUnit unit) {
|
return calculate(date, amount, unit, false);
|
}
|
|
public static DateEntity getDateEntity(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
DateEntity entity = new DateEntity();
|
entity.setYear(calendar.get(Calendar.YEAR));
|
entity.setMonth(calendar.get(Calendar.MONTH));
|
entity.setDay(calendar.get(Calendar.DAY_OF_MONTH));
|
entity.setHour(calendar.get(Calendar.HOUR_OF_DAY));
|
entity.setMinute(calendar.get(Calendar.MINUTE));
|
entity.setSecond(calendar.get(Calendar.SECOND));
|
return entity;
|
}
|
|
public static class DateEntity {
|
private int year;
|
private int month;
|
private int day;
|
private int hour;
|
private int minute;
|
private int second;
|
|
public int getYear() {
|
return year;
|
}
|
|
public void setYear(int year) {
|
this.year = year;
|
}
|
|
public int getMonth() {
|
return month;
|
}
|
|
public void setMonth(int month) {
|
this.month = month;
|
}
|
|
public int getDay() {
|
return day;
|
}
|
|
public void setDay(int day) {
|
this.day = day;
|
}
|
|
public int getHour() {
|
return hour;
|
}
|
|
public void setHour(int hour) {
|
this.hour = hour;
|
}
|
|
public int getMinute() {
|
return minute;
|
}
|
|
public void setMinute(int minute) {
|
this.minute = minute;
|
}
|
|
public int getSecond() {
|
return second;
|
}
|
|
public void setSecond(int second) {
|
this.second = second;
|
}
|
}
|
}
|