作者:茫茫人海啊啊啊_574 | 来源:互联网 | 2023-07-26 10:06
importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.ArrayLis
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
public class MorliaDateUtil {private static final String FORMAT_1 &#61; "yyyy-MM-dd HH:mm:ss";private static final String FORMAT_2 &#61; "yyyyMMddHHmmss";private static final String FORMAT_3 &#61; "yyyyMMddHHmmssSSS";public static String getUTCTimeStr() {return getTime(0, FORMAT_1);} public static String getUTCTimeStr2() {return getTime(0, FORMAT_2);}public static String getCCTTimeStr2() {return getTime(8, FORMAT_2);}public static String getCCTTimeStr3() {return getTime(8, FORMAT_3);}public static String getTime(int timeZoneInt, String formatString) {if(timeZoneInt > 12 || timeZoneInt < -12) {return null;}TimeZone timeZone &#61; TimeZone.getTimeZone("GMT" &#43; (timeZoneInt > 0 ? "&#43;" &#43; timeZoneInt : timeZoneInt) &#43; ":00");Calendar cal &#61; Calendar.getInstance(timeZone);int year &#61; cal.get(Calendar.YEAR);int month &#61; cal.get(Calendar.MONTH) &#43; 1;int day &#61; cal.get(Calendar.DAY_OF_MONTH);int hour &#61; cal.get(Calendar.HOUR_OF_DAY);int minute &#61; cal.get(Calendar.MINUTE);int second &#61; cal.get(Calendar.SECOND);int millisecond &#61; cal.get(Calendar.MILLISECOND);formatString &#61; formatString.replace("yyyy", String.valueOf(year));formatString &#61; formatString.replace("MM", String.valueOf(month > 9 ? month : "0" &#43; month));formatString &#61; formatString.replace("dd", String.valueOf(day > 9 ? day : "0" &#43; day));formatString &#61; formatString.replace("HH", String.valueOf(hour > 9 ? hour : "0" &#43; hour));formatString &#61; formatString.replace("mm", String.valueOf(minute > 9 ? minute : "0" &#43; minute));formatString &#61; formatString.replace("ss", String.valueOf(second > 9 ? second : "0" &#43; second));formatString &#61; formatString.replace("SSS", String.valueOf(millisecond > 9 ? millisecond > 99 ? "0" &#43; millisecond : "00" &#43; millisecond : millisecond));return formatString;}public static List<String> getDateList(String starttime, String endtime){SimpleDateFormat sdf &#61; new SimpleDateFormat("yyyy-MM-dd");Date dateFormat1 &#61; new Date(-7 * 24 * 3600 * 1000L);TimeZone timeZone &#61; TimeZone.getTimeZone("GMT&#43;8:00");SimpleDateFormat currentTime &#61; new SimpleDateFormat("yyyy-MM-dd");currentTime.setTimeZone(timeZone);String currentDate &#61; currentTime.format(new Date());Date dateFormat2 &#61; new Date();try {dateFormat1 &#61; sdf.parse(starttime);dateFormat2 &#61; sdf.parse(endtime).getTime() > sdf.parse(currentDate).getTime()?sdf.parse(currentDate) : sdf.parse(endtime);} catch (ParseException e) {return null;}List<String> list &#61; new ArrayList<String>();while(dateFormat1.getTime() <&#61; dateFormat2.getTime()) {String strs &#61; sdf.format(dateFormat1);list.add(strs);dateFormat1.setTime(dateFormat1.getTime() &#43; 24 * 3600 * 1000L);}return list;}
}
package com.aaa.bbb.utils;import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
&#64;Slf4j
public class ItemDateUtils {private static final String yyyyMMddHHmmssSSS &#61; "yyyyMMddHHmmssSSS";private static final String yyyy_MM_dd_HH_mm_ss&#61;"yyyy-MM-dd HH:mm:ss";private static final String defaultformate&#61;"yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSZ";public static String get8TimeStr() {return getTime(8, yyyy_MM_dd_HH_mm_ss);}public static Date get8Time(){try {SimpleDateFormat simpleDateFormat &#61; new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT&#43;:08:00"));Date dateValue &#61; simpleDateFormat.parse(get8TimeStr());return dateValue;}catch (Exception e){log.error(e.getMessage(),e);}return null;}public static String getTime(int timeZoneInt, String formatString) {if(timeZoneInt > 12 || timeZoneInt < -12) {return null;}TimeZone timeZone &#61; TimeZone.getTimeZone("GMT" &#43; (timeZoneInt > 0 ? "&#43;" &#43; timeZoneInt : timeZoneInt) &#43; ":00");Calendar cal &#61; Calendar.getInstance(timeZone);int year &#61; cal.get(Calendar.YEAR);int month &#61; cal.get(Calendar.MONTH) &#43; 1;int day &#61; cal.get(Calendar.DAY_OF_MONTH);int hour &#61; cal.get(Calendar.HOUR_OF_DAY);int minute &#61; cal.get(Calendar.MINUTE);int second &#61; cal.get(Calendar.SECOND);int millisecond &#61; cal.get(Calendar.MILLISECOND);formatString &#61; formatString.replace("yyyy", String.valueOf(year));formatString &#61; formatString.replace("MM", String.valueOf(month > 9 ? month : "0" &#43; month));formatString &#61; formatString.replace("dd", String.valueOf(day > 9 ? day : "0" &#43; day));formatString &#61; formatString.replace("HH", String.valueOf(hour > 9 ? hour : "0" &#43; hour));formatString &#61; formatString.replace("mm", String.valueOf(minute > 9 ? minute : "0" &#43; minute));formatString &#61; formatString.replace("ss", String.valueOf(second > 9 ? second : "0" &#43; second));formatString &#61; formatString.replace("SSS", String.valueOf(millisecond > 9 ?millisecond > 99 ? "0" &#43; millisecond : "00" &#43; millisecond : millisecond));return formatString;}}