feat: There is no way the,

This commit is contained in:
super_liu
2022-01-06 18:02:47 +08:00
parent 5fdb04f96f
commit fe2949edd5
3 changed files with 163 additions and 5 deletions
@@ -0,0 +1,124 @@
package com.adc.da.search.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @Description: 判断是否为日期格式
* @Author: super_liu
* date: 2020/12/31 11:25
*/
public class DateUtil {
/**
* 取起始时间到结束时间 所有的时间
* @param startTime
* @param endTime
* @return
*/
public static List<String> getAllDate(String startTime, String endTime) {
List<String> list = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(sdf.parse(startTime));
for (long d = cal.getTimeInMillis(); d <= sdf.parse(endTime).getTime(); d = get_D_Plaus_1(cal)) {
list.add(sdf.format(d));
}
} catch (ParseException e) {
System.out.println("date change err");
}
return list;
}
public static long get_D_Plaus_1(Calendar c) {
c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + 1);
return c.getTimeInMillis();
}
/***
* @Description: 判断多个日期是否都符合日期格式
* @Author: super_liu
* @Date: 2020/12/31 11:31
* @Param: [str]
* @Return: boolean
*/
public static boolean isValidDate(String str) throws Exception{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String[] strArr = str.split(",");
for (String dateStr : strArr) {
try {
simpleDateFormat.setLenient(false);
simpleDateFormat.parse(dateStr);
} catch (Exception e){
return false;
}
}
return true;
}
/**
* utc 时间格式转换正常格式 2018-08-07T03:41:59Z
* @param utcTime 时间
* @return
*/
public static String formatStrUTCToDateStr(String utcTime) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.SIMPLIFIED_CHINESE);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
TimeZone utcZone = TimeZone.getTimeZone("UTC");
sf.setTimeZone(utcZone);
Date date = null;
String dateTime = "";
try {
date = sf.parse(utcTime);
dateTime = sdf.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return dateTime;
}
/**
* 将日期对象转换为指定的日期字符串
*
* @param date 传入的日期对象
* @param format 格式
* @return 日期字符串
* @author 赵肖云
*/
public static String dateToString(Date date, String format) {
String string = "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
if (date != null) {
string = simpleDateFormat.format(date);
}
return string;
}
/**
* 将日期字符串转换为一个日期对象
*
* @param datestr 日期字符串
* @param format 格式
* @return Date
* @author 赵肖云
*/
public static Date stingToDate(String datestr, String format) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.parse(datestr);
}
public static void main(String[] args) {
String utcTime = "2021-04-15T01:43:54.296Z";
String time = formatStrUTCToDateStr("2021-04-15T01:43:54.296Z");
System.out.println("utcTime 转换前:" + utcTime);
System.out.println("utcTime 转换后 time " + time);
}
}