fix: 优化DateUtil工具类
This commit is contained in:
parent
2559721f48
commit
48f56e7509
|
@ -1,6 +1,7 @@
|
|||
package site.cnkj.common.utils.date;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
|
@ -8,66 +9,64 @@ import java.text.SimpleDateFormat;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/*
|
||||
* @version 1.0 created by LXW on 2018/11/8 16:11
|
||||
/**
|
||||
* 日期工具类
|
||||
*
|
||||
* @author Liu XueWen, 2021/12/13
|
||||
*/
|
||||
public class DateUtil {
|
||||
|
||||
/**
|
||||
* 日期格式枚举值
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum FORMAT{
|
||||
BASE_TIME_yyyy("yyyy"),
|
||||
BASE_TIME_MM("MM"),
|
||||
BASE_TIME_dd("dd"),
|
||||
BASE_TIME_YEAR("yyyy"),
|
||||
BASE_TIME_MONTH("MM"),
|
||||
BASE_TIME_DAY("dd"),
|
||||
BASE_TIME_HH("HH"),
|
||||
BASE_TIME_mm("mm"),
|
||||
BASE_TIME_ss("ss"),
|
||||
BASE_TIME_SSS("SSS"),
|
||||
FULL_TIME_yMd("yyyy-MM-dd"),
|
||||
FULL_TIME_yMdH("yyyy-MM-dd HH"),
|
||||
FULL_TIME_yMdHm("yyyy-MM-dd HH:mm"),
|
||||
FULL_TIME_yMdHms("yyyy-MM-dd HH:mm:ss"),
|
||||
FULL_TIME_yMdHmsS("yyyy-MM-dd HH:mm:ss.SSS"),
|
||||
FULL_TIME_yM("yyyy-MM"),
|
||||
FULL_TIME_Md("MM-dd"),
|
||||
FULL_TIME_Hms("HH:mm:ss"),
|
||||
FULL_TIME_Hm("HH:mm"),
|
||||
FULL_TIME_ms("mm:ss"),
|
||||
FULL_TIME_HmsS("HH:mm:ss.SSS"),
|
||||
NO_SEGMENTATION_yM("yyyyMM"),
|
||||
NO_SEGMENTATION_yMd("yyyyMMdd"),
|
||||
NO_SEGMENTATION_Hm("HHmm"),
|
||||
NO_SEGMENTATION_Hms("HHmmss"),
|
||||
NO_SEGMENTATION_HmsS("HHmmssSSS"),
|
||||
NO_SEGMENTATION_yMdHm("yyyyMMddHHmm"),
|
||||
NO_SEGMENTATION_yMdHms("yyyyMMddHHmmss"),
|
||||
NO_SEGMENTATION_yMdHmsS("yyyyMMddHHmmssSSS"),
|
||||
POINT_yMd("yyyy.MM.dd"),
|
||||
POINT_Hms("HH.mm.ss"),
|
||||
FULL_TIME_FORMAT_yMd("yyyy年MM月dd日"),
|
||||
FULL_TIME_FORMAT_Hms("HH时mm分ss秒"),
|
||||
FULL_TIME_FORMAT_yMdHms("yyyy年MM月dd日 HH时mm分ss秒"),
|
||||
FULL_TIME_FORMAT_yMdHmsS("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒");
|
||||
|
||||
FORMAT(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
BASE_TIME_MINUTES("mm"),
|
||||
BASE_TIME_SECONDS("ss"),
|
||||
BASE_TIME_MILLIONS_SECONDS("SSS"),
|
||||
FULL_TIME_YMD("yyyy-MM-dd"),
|
||||
FULL_TIME_YMD_H("yyyy-MM-dd HH"),
|
||||
FULL_TIME_YMD_HM("yyyy-MM-dd HH:mm"),
|
||||
FULL_TIME_YMD_HMS("yyyy-MM-dd HH:mm:ss"),
|
||||
FULL_TIME_YMD_HMS_S("yyyy-MM-dd HH:mm:ss.SSS"),
|
||||
FULL_TIME_YM("yyyy-MM"),
|
||||
FULL_TIME_MD("MM-dd"),
|
||||
FULL_TIME_HMS("HH:mm:ss"),
|
||||
FULL_TIME_HM("HH:mm"),
|
||||
FULL_TIME_MS("mm:ss"),
|
||||
FULL_TIME_HMS_S("HH:mm:ss.SSS"),
|
||||
NO_SEGMENTATION_YM("yyyyMM"),
|
||||
NO_SEGMENTATION_YMD("yyyyMMdd"),
|
||||
NO_SEGMENTATION_HM("HHmm"),
|
||||
NO_SEGMENTATION_HMS("HHmmss"),
|
||||
NO_SEGMENTATION_HMS_S("HHmmssSSS"),
|
||||
NO_SEGMENTATION_YMD_HM("yyyyMMddHHmm"),
|
||||
NO_SEGMENTATION_YMD_HMS("yyyyMMddHHmmss"),
|
||||
NO_SEGMENTATION_YMD_HMS_S("yyyyMMddHHmmssSSS"),
|
||||
POINT_YMD("yyyy.MM.dd"),
|
||||
POINT_HMS("HH.mm.ss"),
|
||||
FULL_TIME_FORMAT_YMD("yyyy年MM月dd日"),
|
||||
FULL_TIME_FORMAT_HMS("HH时mm分ss秒"),
|
||||
FULL_TIME_FORMAT_YMD_HMS("yyyy年MM月dd日 HH时mm分ss秒"),
|
||||
FULL_TIME_FORMAT_YMD_HMS_S("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒");
|
||||
|
||||
private final String value;
|
||||
|
||||
public String getValue(){
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期字符串转日期格式
|
||||
* @param date 日期字符串
|
||||
* @param timeFormat 转换后的日期格式
|
||||
* @return
|
||||
* @return date
|
||||
*/
|
||||
public static Date dateStringToDate(String date, String timeFormat){
|
||||
try {
|
||||
|
@ -81,8 +80,14 @@ public class DateUtil {
|
|||
return null;
|
||||
}
|
||||
|
||||
public static String timeStamp2fulltime(Date date){
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(FORMAT.FULL_TIME_yMdHms.getValue());
|
||||
/**
|
||||
* 按照yyyy-MM-dd HH:mm:ss 格式格式化日期对象
|
||||
*
|
||||
* @param date 日期对象
|
||||
* @return 日期字符串
|
||||
*/
|
||||
public static String dateToString(Date date){
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(FORMAT.FULL_TIME_YMD_HMS.getValue());
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
|
@ -94,9 +99,14 @@ public class DateUtil {
|
|||
* @return 零点时间戳(0:0:0)
|
||||
*/
|
||||
public static long getTodayEarlyMorning(String time, String timeFormat){
|
||||
long current=translateDateToTimestamp(time, timeFormat);//当前时间毫秒数
|
||||
long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//前一天的零点零分零秒的毫秒数
|
||||
return zero + 24*60*60*1000;
|
||||
//当前时间毫秒数
|
||||
Long current = translateDateToTimestamp(time, timeFormat);
|
||||
if (null != current){
|
||||
//前一天的零点零分零秒的毫秒数
|
||||
long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();
|
||||
return zero + 24*60*60*1000;
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
|
||||
|
||||
|
@ -108,9 +118,14 @@ public class DateUtil {
|
|||
* @return 当天24点的时间戳(23:59:59)
|
||||
*/
|
||||
public static long getTodayLaterMorning(String time, String timeFormat){
|
||||
long current=translateDateToTimestamp(time, timeFormat);//当前时间毫秒数
|
||||
long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//前一天的零点零分零秒的毫秒数
|
||||
return zero + 24*60*60*1000*2 - 1;
|
||||
//当前时间毫秒数
|
||||
Long current=translateDateToTimestamp(time, timeFormat);
|
||||
if (null != current){
|
||||
//前一天的零点零分零秒的毫秒数
|
||||
long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();
|
||||
return zero + 24*60*60*1000*2 - 1;
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
|
||||
|
||||
|
@ -128,7 +143,7 @@ public class DateUtil {
|
|||
calendar.set(Calendar.SECOND, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
long time = calendar.getTime().getTime();
|
||||
return time - 24*60*60*1000*(maxDays);
|
||||
return time - 24L * 60 * 60 * 1000 * maxDays;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -136,35 +151,51 @@ public class DateUtil {
|
|||
* @param pastTime 指定的时间数量 5
|
||||
* @param type 时间的类型 d日 H时 m分 s秒 S毫秒 不写默认单位秒
|
||||
* @param timeFormat 转换后的时间戳格式化的标准 不写默认格式 年-月-日 时:分:秒
|
||||
* @return
|
||||
* @return 过去指定时间的时间戳
|
||||
*/
|
||||
public static String getThePastTime(int pastTime, String type, String timeFormat){
|
||||
public static String getThePastTime(long pastTime, String type, String timeFormat){
|
||||
if (type == null){
|
||||
type = "s";
|
||||
}
|
||||
if (timeFormat == null){
|
||||
timeFormat = FORMAT.FULL_TIME_yMdHmsS.getValue();
|
||||
timeFormat = FORMAT.FULL_TIME_YMD_HMS_S.getValue();
|
||||
}
|
||||
long date = System.currentTimeMillis();
|
||||
Long now_time = new Long(1);
|
||||
long nowTime = 1L;
|
||||
if ("S".equals(type)){
|
||||
now_time = date - pastTime;
|
||||
nowTime = date - pastTime;
|
||||
}else if ("s".equals(type)){
|
||||
now_time = date - pastTime*1000;
|
||||
nowTime = date - pastTime* 1000L;
|
||||
}else if ("m".equals(type)){
|
||||
now_time = date - pastTime*1000*60;
|
||||
nowTime = date - pastTime *1000*60;
|
||||
}else if ("H".equals(type)){
|
||||
now_time = date - pastTime*1000*60*60;
|
||||
nowTime = date - pastTime *1000*60*60;
|
||||
}else if ("d".equals(type)){
|
||||
now_time = date - pastTime*1000*60*60*24;
|
||||
nowTime = date - pastTime *1000*60*60*24;
|
||||
}
|
||||
return translateTimeToDate(now_time, timeFormat);
|
||||
return translateTimeToDate(nowTime, timeFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取过去指定时间的时间戳
|
||||
* 默认日期格式: com.acca.opra.mch.utils.DateUtils.FORMAT.FULL_TIME_YMD_HMS_S
|
||||
* 默认日期级别: 秒 s
|
||||
*
|
||||
* @param pastTime 指定的时间数量 5
|
||||
* @return 过去指定时间的时间戳
|
||||
*/
|
||||
public static String getThePastTime(int pastTime){
|
||||
return getThePastTime(pastTime, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取过去指定时间的时间戳
|
||||
* 默认日期格式: com.acca.opra.mch.utils.DateUtils.FORMAT.FULL_TIME_YMD_HMS_S
|
||||
*
|
||||
* @param pastTime 指定的时间数量 5
|
||||
* @param type 时间的类型 d日 H时 m分 s秒 S毫秒 不写默认单位秒
|
||||
* @return 过去指定时间的时间戳
|
||||
*/
|
||||
public static String getThePastTime(int pastTime, String type){
|
||||
return getThePastTime(pastTime, type, null);
|
||||
}
|
||||
|
@ -177,9 +208,12 @@ public class DateUtil {
|
|||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取指定格式的当前时间
|
||||
* @return 时间
|
||||
*
|
||||
* @param timeFormat 日期格式
|
||||
* @return 当前时间
|
||||
*/
|
||||
public static String getNowTimeByFormat(String timeFormat){
|
||||
Date date = new Date();
|
||||
|
@ -230,7 +264,7 @@ public class DateUtil {
|
|||
* 13位时间戳转date格式
|
||||
* @param timestamp 时间戳
|
||||
* @param timeFormat 时间格式
|
||||
* @return
|
||||
* @return .
|
||||
*/
|
||||
public static Date translateTimestampToDate(Long timestamp, String timeFormat){
|
||||
try {
|
||||
|
@ -291,51 +325,20 @@ public class DateUtil {
|
|||
* @param timeTranslate 输出格式
|
||||
* @return 日期集合
|
||||
*/
|
||||
public static List getDesignationDay(int num, String time, String timeFormat, String timeTranslate){
|
||||
List days = new ArrayList();
|
||||
Long timestamp_time = translateDateToTimestamp(time, timeFormat);
|
||||
for (int i = 0; i < num; i++) {
|
||||
Long new_time = timestamp_time - i*24*60*60*1000;
|
||||
String today = translateTimeToDate(new_time, timeTranslate);
|
||||
days.add(today);
|
||||
public static List<String> getDesignationDay(int num, String time, String timeFormat, String timeTranslate){
|
||||
List<String> days = new ArrayList<>();
|
||||
Long timestampTime = translateDateToTimestamp(time, timeFormat);
|
||||
if (null != timestampTime){
|
||||
for (int i = 0; i < num; i++) {
|
||||
long newTime = timestampTime - (long) i *24*60*60*1000;
|
||||
String today = translateTimeToDate(newTime, timeTranslate);
|
||||
days.add(today);
|
||||
}
|
||||
}
|
||||
return days;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间切割机,小时切割
|
||||
*
|
||||
* @param cutTime 切割小时数 2小时 = 2
|
||||
* @param startTime 开始时间戳 10位
|
||||
* @param endTime 结束时间戳 10位
|
||||
* @return 切割后时间段列表
|
||||
*/
|
||||
public static List<String> splitTimestamp(int cutTime, long startTime, long endTime) {
|
||||
List<String> timeList = new ArrayList<>();
|
||||
startTime = startTime * 1000;
|
||||
endTime = endTime * 1000;
|
||||
if (cutTime <= 0){
|
||||
timeList.add(String.valueOf(String.valueOf(startTime).concat(",").concat(String.valueOf(endTime))));
|
||||
}else if (cutTime > 0 && endTime > startTime){
|
||||
long cut = ((endTime - startTime) / 3600000 /cutTime) + ((endTime - startTime) / 3600000 % cutTime);
|
||||
long timeInterval = (endTime - startTime) / cut;
|
||||
while(true){
|
||||
if (startTime < endTime){
|
||||
if ((startTime + timeInterval) > endTime){
|
||||
timeList.add(String.valueOf(String.valueOf(startTime).concat(",").concat(String.valueOf(endTime))));
|
||||
}else {
|
||||
timeList.add(String.valueOf( String.valueOf(startTime).concat(",").concat(String.valueOf(startTime + timeInterval))));
|
||||
}
|
||||
}else if (startTime >= endTime){
|
||||
break;
|
||||
}
|
||||
startTime = startTime + timeInterval;
|
||||
}
|
||||
}
|
||||
return timeList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前时间与周期时间的时间差
|
||||
* 最大支持3600秒
|
||||
|
@ -344,58 +347,17 @@ public class DateUtil {
|
|||
* @param executorTime 执行周期
|
||||
* @return 等待秒
|
||||
*/
|
||||
public static long waitingTimeBySeconds(int executorTime){
|
||||
public static long waitingTimeBySeconds(long executorTime){
|
||||
if (executorTime <= 3600 && executorTime >=0){
|
||||
//获取当前时间与周期时间的差
|
||||
long nowTime = System.currentTimeMillis();
|
||||
int timeDiscrepancy = Integer.parseInt(String.valueOf(nowTime%(executorTime*1000)/1000));
|
||||
long timeDiscrepancy = Integer.parseInt(String.valueOf(nowTime%(executorTime*1000)/1000));
|
||||
return executorTime-timeDiscrepancy;
|
||||
}else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 等待开始周期执行的时间
|
||||
* 最大限制24h
|
||||
* 单位 小时
|
||||
*
|
||||
* @param executorTime 周期
|
||||
* @return 等待秒
|
||||
*/
|
||||
public static long waitingTimeByHours(int executorTime){
|
||||
long delayTime = 0;
|
||||
if (executorTime <= 24 && executorTime>0){
|
||||
int num = 24 / executorTime;
|
||||
long tomorrowEarlyMorning = getTodayEarlyMorning(translateTimeToDate(getCurrentTime(), FORMAT.FULL_TIME_yMdHmsS.getValue()), FORMAT.FULL_TIME_yMdHmsS.getValue());
|
||||
long todayEarlyMorning = tomorrowEarlyMorning - 24*60*60*1000;
|
||||
long todayHour = num * executorTime * 3600000 + todayEarlyMorning;
|
||||
if (todayHour < getCurrentTime() && getCurrentTime() < tomorrowEarlyMorning){
|
||||
delayTime = (tomorrowEarlyMorning - getCurrentTime()) / 1000 / 60;
|
||||
}else {
|
||||
for (int i = 0; i <= num; i++) {
|
||||
long current = i * executorTime * 3600000 + todayEarlyMorning;
|
||||
if (getCurrentTime() == current){
|
||||
delayTime = 0;
|
||||
break;
|
||||
}else if (current > getCurrentTime()){
|
||||
delayTime = (current - getCurrentTime()) / 1000 / 60;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else {
|
||||
delayTime = 0;
|
||||
}
|
||||
if (delayTime <= 0){
|
||||
return 0;
|
||||
}else {
|
||||
delayTime = delayTime * 60 + waitingTimeBySeconds(60);
|
||||
return delayTime;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断传入的字符串是时间格式还是时间戳格式
|
||||
* 时间戳格式根据设置的format转换成对应的时间格式
|
||||
|
@ -408,12 +370,17 @@ public class DateUtil {
|
|||
String time = "";
|
||||
if (date.length() == 14){
|
||||
//20190613171965
|
||||
time = date.substring(0, 4) + "-" +
|
||||
date.substring(4, 6) + "-" +
|
||||
date.substring(6, 8) + " " +
|
||||
date.substring(8, 10) + ":" +
|
||||
date.substring(10, 12) + ":" +
|
||||
date.substring(12, 14);
|
||||
time = date.substring(0, 4)
|
||||
+ "-"
|
||||
+ date.substring(4, 6)
|
||||
+ "-"
|
||||
+ date.substring(6, 8)
|
||||
+ " "
|
||||
+ date.substring(8, 10)
|
||||
+ ":"
|
||||
+ date.substring(10, 12)
|
||||
+ ":"
|
||||
+ date.substring(12, 14);
|
||||
}else if (date.length() == 13){
|
||||
//1560390240760
|
||||
time = translateTimeToDate(Long.valueOf(date), format);
|
||||
|
@ -425,15 +392,19 @@ public class DateUtil {
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 时间格式转换
|
||||
* @param time 2020-03-24T11:24:31.000Z
|
||||
* @return 2020-03-24 19:24:31
|
||||
* @throws ParseException e
|
||||
*/
|
||||
public static String switchTime(String time) throws ParseException {
|
||||
SimpleDateFormat format2 = new SimpleDateFormat(FORMAT.FULL_TIME_yMdHms.getValue());
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//格式化的表达式
|
||||
time = time.replace("Z", " UTC");//是空格+UTC
|
||||
SimpleDateFormat format2 = new SimpleDateFormat(FORMAT.FULL_TIME_YMD_HMS_S.getValue());
|
||||
//格式化的表达式
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
|
||||
//是空格+UTC
|
||||
time = time.replace("Z", " UTC");
|
||||
Date data = format.parse(time);
|
||||
return format2.format(data);
|
||||
}
|
||||
|
@ -442,113 +413,29 @@ public class DateUtil {
|
|||
* 转化时间格式
|
||||
* @param time 2020-01-15T15:16:23+08:00
|
||||
* @return 2020-01-15 15:16:23
|
||||
* @throws ParseException
|
||||
* @throws java.text.ParseException e
|
||||
*/
|
||||
public static String switchLineTime(String time) throws ParseException {
|
||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
||||
Date date = df.parse(time);
|
||||
SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
|
||||
Date date = df.parse(time);
|
||||
SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
|
||||
Date date1 = df1.parse(date.toString());
|
||||
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return df2.format(date1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据日期格式自动转换时间
|
||||
* 最大精确到秒,不支持毫秒,最小精确到年,不支持世纪
|
||||
* @param date_format 日期格式,例如 YYYY-MM-dd
|
||||
* @param parseInt 前/后 多少,例如 1 根据date_format格式表达为当天时间的前一天
|
||||
* @return 格式化后的时间
|
||||
*/
|
||||
public static String formatDateByReg(String date_format, int parseInt){
|
||||
String translateTimeToDate = "";
|
||||
Long current_time = System.currentTimeMillis();
|
||||
if ((date_format.contains("YYYY")||date_format.contains("yyyy")) &&
|
||||
date_format.contains("MM") &&
|
||||
date_format.contains("dd") &&
|
||||
date_format.contains("HH") &&
|
||||
date_format.contains("mm") &&
|
||||
date_format.contains("ss")){
|
||||
//秒
|
||||
long real_time = current_time + parseInt * 1000L;
|
||||
translateTimeToDate = translateTimeToDate(real_time, date_format);
|
||||
}else if ((date_format.contains("YYYY")||date_format.contains("yyyy")) &&
|
||||
date_format.contains("MM") &&
|
||||
date_format.contains("dd") &&
|
||||
date_format.contains("HH") &&
|
||||
date_format.contains("mm") &&
|
||||
!date_format.contains("ss")){
|
||||
//分钟
|
||||
long real_time = current_time + (long) parseInt * 60 * 1000;
|
||||
translateTimeToDate = translateTimeToDate(real_time, date_format);
|
||||
}else if ((date_format.contains("YYYY")||date_format.contains("yyyy")) &&
|
||||
date_format.contains("MM") &&
|
||||
date_format.contains("dd") &&
|
||||
date_format.contains("HH") &&
|
||||
!date_format.contains("mm") &&
|
||||
!date_format.contains("ss")){
|
||||
//小时
|
||||
long real_time = current_time + (long) parseInt * 60 * 60 * 1000;
|
||||
translateTimeToDate = translateTimeToDate(real_time, date_format);
|
||||
}else if ((date_format.contains("YYYY")||date_format.contains("yyyy")) &&
|
||||
date_format.contains("MM") &&
|
||||
date_format.contains("dd") &&
|
||||
!date_format.contains("HH") &&
|
||||
!date_format.contains("mm") &&
|
||||
!date_format.contains("ss")){
|
||||
//天
|
||||
long real_time = current_time + (long) parseInt * 24 * 60 * 60 * 1000;
|
||||
translateTimeToDate = translateTimeToDate(real_time, date_format);
|
||||
}else if ((date_format.contains("YYYY")||date_format.contains("yyyy")) &&
|
||||
date_format.contains("MM") &&
|
||||
!date_format.contains("dd") &&
|
||||
!date_format.contains("HH") &&
|
||||
!date_format.contains("mm") &&
|
||||
!date_format.contains("ss")){
|
||||
//月
|
||||
String toDate = translateTimeToDate(current_time, FORMAT.FULL_TIME_yM.getValue());
|
||||
Integer year = Integer.valueOf(toDate.split("-")[0]);
|
||||
Integer month = Integer.valueOf(toDate.split("-")[1]);
|
||||
int shang = parseInt / 12;
|
||||
int yu = parseInt % 12;
|
||||
year = year + shang;
|
||||
month = month + yu;
|
||||
if (month<0){
|
||||
year = year - 1;
|
||||
month = 12 + month;
|
||||
}else if (month>12){
|
||||
year = year + 1;
|
||||
month = month - 12;
|
||||
}
|
||||
String i5 = month<10?"0"+String.valueOf(month):String.valueOf(month);
|
||||
Long translateDateToTimestamp = translateDateToTimestamp(String.valueOf(year) + "-" + i5, FORMAT.FULL_TIME_yM.getValue());
|
||||
translateTimeToDate = translateTimeToDate(translateDateToTimestamp, date_format);
|
||||
}else if ((date_format.contains("YYYY")||date_format.contains("yyyy")) &&
|
||||
!date_format.contains("MM") &&
|
||||
!date_format.contains("dd") &&
|
||||
!date_format.contains("HH") &&
|
||||
!date_format.contains("mm") &&
|
||||
!date_format.contains("ss")){
|
||||
//年
|
||||
String year = translateTimeToDate(current_time, FORMAT.BASE_TIME_yyyy.getValue());
|
||||
String value = String.valueOf(Integer.valueOf(year) + parseInt);
|
||||
Long aLong = translateDateToTimestamp(value, FORMAT.BASE_TIME_yyyy.getValue());
|
||||
translateTimeToDate = translateTimeToDate(aLong, date_format);
|
||||
}else {
|
||||
translateTimeToDate = translateTimeToDate(current_time, date_format);
|
||||
}
|
||||
return translateTimeToDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换String类型的Date格式数据为指定格式
|
||||
*
|
||||
* @param time Date 格式日期字符串
|
||||
* @param date_format 格式化后类型
|
||||
* @param dateFormat 格式化后类型
|
||||
* @return 格式化后字符串
|
||||
* @throws ParseException e
|
||||
*/
|
||||
public static String changeDateStringToFormatString(String time, String date_format) throws Exception{
|
||||
public static String changeDateStringToFormatString(String time, String dateFormat) throws ParseException {
|
||||
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
|
||||
SimpleDateFormat format2 = new SimpleDateFormat(date_format);
|
||||
SimpleDateFormat format2 = new SimpleDateFormat(dateFormat);
|
||||
Date data = format.parse(time);
|
||||
return format2.format(data);
|
||||
}
|
||||
|
@ -562,9 +449,9 @@ public class DateUtil {
|
|||
public static String getFirstDayByMonth(int month, String format){
|
||||
//获取当前月第一天:
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.clear();
|
||||
c.set(Calendar.MONTH, month);
|
||||
c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
|
||||
//设置为1号,当前日期既为本月第一天
|
||||
c.set(Calendar.DAY_OF_MONTH, 1);
|
||||
return translateDateToString(c.getTime(), format);
|
||||
}
|
||||
|
||||
|
@ -578,7 +465,6 @@ public class DateUtil {
|
|||
public static String getFirstDayByMonth(int year, int month, String format){
|
||||
//获取当前月最后一天
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.clear();
|
||||
c.set(Calendar.YEAR, year);
|
||||
c.set(Calendar.MONTH, month);
|
||||
c.set(Calendar.DAY_OF_MONTH, 1);
|
||||
|
@ -594,41 +480,11 @@ public class DateUtil {
|
|||
public static String getLastDayByMonth(int month, String format){
|
||||
//获取当前月最后一天
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.clear();
|
||||
c.set(Calendar.MONTH, month);
|
||||
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
|
||||
return translateDateToString(c.getTime(), format);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按小時切割时间段
|
||||
* @param startTime 开始时间 yyyy-MM-dd HH:mm:ss
|
||||
* @param endTime 结束时间 yyyy-MM-dd HH:mm:ss
|
||||
* @return 时间段集合
|
||||
* [
|
||||
* {
|
||||
* "startTime": Date,
|
||||
* "endTime": Date
|
||||
* }
|
||||
* ]
|
||||
*/
|
||||
public static List<JSONObject> getAroundTimeList(String startTime, String endTime, int cut){
|
||||
List<JSONObject> times = new LinkedList<>();
|
||||
Long startTimestamp = translateDateToTimestamp(startTime, FORMAT.FULL_TIME_yMdHms.getValue())/1000;
|
||||
Long endTimestamp = translateDateToTimestamp(endTime, FORMAT.FULL_TIME_yMdHms.getValue())/1000;
|
||||
List<String> list = splitTimestamp(cut, startTimestamp, endTimestamp);
|
||||
for (String s : list) {
|
||||
String[] split = s.split(",");
|
||||
Date start_time = translateTimestampToDate(Long.valueOf(split[0]), FORMAT.FULL_TIME_yMdHms.getValue());
|
||||
Date end_time = translateTimestampToDate(Long.valueOf(split[1]), FORMAT.FULL_TIME_yMdHms.getValue());
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("startTime", start_time);
|
||||
jsonObject.put("endTime", end_time);
|
||||
times.add(jsonObject);
|
||||
}
|
||||
return times;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算月份区间
|
||||
*
|
||||
|
@ -638,7 +494,10 @@ public class DateUtil {
|
|||
* @param outputFormat 输出日期格式
|
||||
* @return 月份区间集合
|
||||
*/
|
||||
public static List<String> computerMonth(String startMonth, String endMonth, String inputFormat, String outputFormat) {
|
||||
public static List<String> computerMonth(String startMonth,
|
||||
String endMonth,
|
||||
String inputFormat,
|
||||
String outputFormat) {
|
||||
List<String> months = new ArrayList<>();
|
||||
Long startTimestamp = translateDateToTimestamp(startMonth, inputFormat);
|
||||
Long endTimestamp = translateDateToTimestamp(endMonth, inputFormat);
|
||||
|
@ -768,3 +627,4 @@ public class DateUtil {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -278,12 +278,12 @@ public class FileUtil {
|
|||
}
|
||||
|
||||
public static File getRecoverFileName(String templateName, String paramsNum, String path) throws Exception{
|
||||
String translateTimeToDate = DateUtil.translateTimeToDate(DateUtil.getLastDaysTimestamp(1), DateUtil.FORMAT.NO_SEGMENTATION_yMd.getValue());
|
||||
String translateTimeToDate = DateUtil.translateTimeToDate(DateUtil.getLastDaysTimestamp(1), DateUtil.FORMAT.NO_SEGMENTATION_YMD.getValue());
|
||||
List<String> fileNames = Arrays.asList(
|
||||
templateName,
|
||||
translateTimeToDate,
|
||||
"00",
|
||||
DateUtil.translateTimeToDate(System.currentTimeMillis(), DateUtil.FORMAT.NO_SEGMENTATION_yMdHms.getValue()),
|
||||
DateUtil.translateTimeToDate(System.currentTimeMillis(), DateUtil.FORMAT.NO_SEGMENTATION_YMD_HMS.getValue()),
|
||||
HttpCommonUtil.hostName,
|
||||
paramsNum
|
||||
);
|
||||
|
@ -297,14 +297,14 @@ public class FileUtil {
|
|||
|
||||
public static String getLogFileName(String templateName, String paramsNum, String date_format, int date_num) throws Exception{
|
||||
if (StringUtils.isEmpty(date_format)){
|
||||
date_format = DateUtil.FORMAT.NO_SEGMENTATION_yMdHms.getValue();
|
||||
date_format = DateUtil.FORMAT.NO_SEGMENTATION_YMD_HMS.getValue();
|
||||
}
|
||||
String translateTimeToDate = DateUtil.translateTimeToDate(DateUtil.getLastDaysTimestamp(date_num), date_format);
|
||||
List<String> fileNames = Arrays.asList(
|
||||
templateName,
|
||||
translateTimeToDate,
|
||||
"00",
|
||||
DateUtil.translateTimeToDate(System.currentTimeMillis(), DateUtil.FORMAT.NO_SEGMENTATION_yMdHmsS.getValue()),
|
||||
DateUtil.translateTimeToDate(System.currentTimeMillis(), DateUtil.FORMAT.NO_SEGMENTATION_YMD_HMS_S.getValue()),
|
||||
HttpCommonUtil.hostName,
|
||||
paramsNum
|
||||
);
|
||||
|
|
|
@ -71,7 +71,7 @@ public class LoggerUtil {
|
|||
private String traceId = getMDC();
|
||||
private String requestId = MDC.get("requestId");
|
||||
private String hostName = HOST_NAME;//机器名
|
||||
private String time = DateUtil.translateDateToString(new Date(), DateUtil.FORMAT.FULL_TIME_yMdHmsS.getValue());//时间
|
||||
private String time = DateUtil.translateDateToString(new Date(), DateUtil.FORMAT.FULL_TIME_YMD_HMS_S.getValue());//时间
|
||||
private Long timestamp = new Date().getTime();
|
||||
private String className = new Throwable().getStackTrace()[4].getClassName();
|
||||
private String methodName = new Throwable().getStackTrace()[4].getMethodName();
|
||||
|
|
Loading…
Reference in New Issue
Block a user