package com.se.system.utils;
|
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.time.ZoneId;
|
import java.time.format.DateTimeFormatter;
|
import java.util.Calendar;
|
import java.util.Date;
|
|
@SuppressWarnings("ALL")
|
public class DateUtils {
|
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
|
|
private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
private static Calendar calendar = Calendar.getInstance();
|
|
public DateUtils() {
|
}
|
|
public static synchronized String getCurrentDateForFile() {
|
long currentTimeMillis = System.currentTimeMillis();
|
Date date = new Date(currentTimeMillis);
|
return sdf.format(date);
|
}
|
|
public static Long getTime(String time) {
|
if (CommonUtils.isEmpty(time)) {
|
System.out.println("时间[" + time + "]格式不合法");
|
return null;
|
} else if (time.length() < 11) {
|
dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
LocalDate parse = LocalDate.parse(time, dtf);
|
return LocalDate.from(parse).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
|
} else {
|
LocalDateTime parse = LocalDateTime.parse(time, dtf);
|
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
|
}
|
}
|
|
public static synchronized String getDate() {
|
Date date = new Date();
|
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
return sdf.format(date);
|
}
|
|
public static synchronized String date2Str(Long time) {
|
Date date = new Date(time);
|
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
return sdf.format(date);
|
}
|
|
public static synchronized String date2Str(Date time) {
|
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
return sdf.format(time);
|
}
|
|
public static synchronized Date str2Date(String time) {
|
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
try {
|
return sdf.parse(time);
|
} catch (Exception var2) {
|
System.out.println("字符串[" + time + "]转换日期格式异常");
|
return null;
|
}
|
}
|
|
public static Date addYear(Date date, int mount) {
|
calendar.setTime(date);
|
calendar.add(1, mount);
|
return calendar.getTime();
|
}
|
|
public static Date addYear(Long time, int mount) {
|
Date date = new Date(time);
|
calendar.setTime(date);
|
calendar.add(1, mount);
|
return calendar.getTime();
|
}
|
|
public static Date addMonth(Date date, int mount) {
|
calendar.setTime(date);
|
calendar.add(2, mount);
|
return calendar.getTime();
|
}
|
|
public static Date addMonth(Long time, int mount) {
|
Date date = new Date(time);
|
calendar.setTime(date);
|
calendar.add(1, mount);
|
return calendar.getTime();
|
}
|
|
public static Date addDay(Date date, int mount) {
|
calendar.setTime(date);
|
calendar.add(5, mount);
|
return calendar.getTime();
|
}
|
|
public static Date addDay(Long time, int mount) {
|
Date date = new Date(time);
|
calendar.setTime(date);
|
calendar.add(5, mount);
|
return calendar.getTime();
|
}
|
|
public static void main(String[] args) {
|
Long time = 1555588742901L;
|
date2Str(time);
|
}
|
}
|