优化时间转换工具类

This commit is contained in:
liuchengqian 2022-05-12 15:58:20 +08:00
parent 2cb11615cd
commit 6593241be0

View File

@ -1,12 +1,16 @@
package com.xkrs.utils; package com.xkrs.utils;
import java.time.*; import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
* 日期时间工具 * 日期时间工具
*
* @author tajochen * @author tajochen
*/ */
public class DateTimeUtil { public class DateTimeUtil {
@ -19,16 +23,26 @@ public class DateTimeUtil {
/** /**
* 字符串转LocalDate * 字符串转LocalDate
*
* @param date * @param date
* @return * @return
*/ */
public static LocalDate stringToDate(String date) { public static LocalDate stringToDate(String date) {
assert date != null; LocalDate parse = null;
return LocalDate.parse(date, COMMON_FORMATTER_DATE); try {
parse = LocalDate.parse(date, COMMON_FORMATTER_DATE);
} catch (Exception e) {
e.printStackTrace();
}
if (parse == null) {
return LocalDate.parse(date, COMMON_FORMATTER_DATETIME);
}
return parse;
} }
/** /**
* LocalDate转字符串 * LocalDate转字符串
*
* @param date * @param date
* @return * @return
*/ */
@ -39,6 +53,7 @@ public class DateTimeUtil {
/** /**
* LocalDateTime转字符串 * LocalDateTime转字符串
*
* @param dateTime * @param dateTime
* @return * @return
*/ */
@ -49,16 +64,26 @@ public class DateTimeUtil {
/** /**
* 字符串转LocalDateTime * 字符串转LocalDateTime
* @param dateStr *
* @param date
* @return * @return
*/ */
public static LocalDateTime stringToDateTime(String dateStr) { public static LocalDateTime stringToDateTime(String date) {
assert dateStr != null; LocalDateTime parse = null;
return LocalDateTime.parse(dateStr, COMMON_FORMATTER_DATETIME); try {
parse = LocalDateTime.parse(date, COMMON_FORMATTER_DATETIME);
} catch (Exception e) {
e.printStackTrace();
}
if (parse == null) {
return LocalDateTime.parse(date, COMMON_FORMATTER_DATE);
}
return parse;
} }
/** /**
* 字符串转Instant时间戳 * 字符串转Instant时间戳
*
* @param str * @param str
* @return * @return
*/ */
@ -69,6 +94,7 @@ public class DateTimeUtil {
/** /**
* LocalDateTime转字符串格式化 * LocalDateTime转字符串格式化
*
* @param dateTime * @param dateTime
* @param formatter * @param formatter
* @return * @return
@ -80,6 +106,7 @@ public class DateTimeUtil {
/** /**
* 字符串转LocalDateTime格式化 * 字符串转LocalDateTime格式化
*
* @param dateStr * @param dateStr
* @param formatter * @param formatter
* @return * @return
@ -91,10 +118,11 @@ public class DateTimeUtil {
/** /**
* 字符串转 local date * 字符串转 local date
*
* @param dateStr * @param dateStr
* @return * @return
*/ */
public static LocalDate stringToDateFormatter(String dateStr){ public static LocalDate stringToDateFormatter(String dateStr) {
LocalDate date = LocalDate.parse(dateStr, COMMON_FORMATTER_DATE); LocalDate date = LocalDate.parse(dateStr, COMMON_FORMATTER_DATE);
return date; return date;
} }
@ -102,6 +130,7 @@ public class DateTimeUtil {
/** /**
* 日期转时间戳 * 日期转时间戳
*
* @param dateTime * @param dateTime
* @return * @return
*/ */
@ -112,6 +141,7 @@ public class DateTimeUtil {
/** /**
* 时间戳转日期 * 时间戳转日期
*
* @param timeMillis * @param timeMillis
* @return * @return
*/ */
@ -122,6 +152,7 @@ public class DateTimeUtil {
/** /**
* 时间戳转时间 * 时间戳转时间
*
* @param timeMillis * @param timeMillis
* @return * @return
*/ */
@ -132,16 +163,18 @@ public class DateTimeUtil {
/** /**
* 时间戳转时间再转字符串 * 时间戳转时间再转字符串
*
* @param timeMillis * @param timeMillis
* @return * @return
*/ */
public static String timeMillisToString(long timeMillis){ public static String timeMillisToString(long timeMillis) {
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeMillis, 0, ZoneOffset.ofHours(8)); LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeMillis, 0, ZoneOffset.ofHours(8));
return COMMON_FORMATTER_DATETIME.format(localDateTime); return COMMON_FORMATTER_DATETIME.format(localDateTime);
} }
/** /**
* 获取当前时间 hh:mm:ss:nnn * 获取当前时间 hh:mm:ss:nnn
*
* @return * @return
*/ */
public static LocalDateTime getNowTime() { public static LocalDateTime getNowTime() {
@ -151,6 +184,7 @@ public class DateTimeUtil {
/** /**
* 获取当前日期 yyyy-MM-dd * 获取当前日期 yyyy-MM-dd
*
* @return * @return
*/ */
public static LocalDate getToday() { public static LocalDate getToday() {
@ -160,6 +194,7 @@ public class DateTimeUtil {
/** /**
* 获取当前 Instant 时间戳 * 获取当前 Instant 时间戳
*
* @return * @return
*/ */
public static Instant getInstant() { public static Instant getInstant() {
@ -169,29 +204,26 @@ public class DateTimeUtil {
/** /**
* 获取当前时间 yyyy-MM-ss hh:mm:ss * 获取当前时间 yyyy-MM-ss hh:mm:ss
*
* @return * @return
*/ */
public static String getNowTimeStr(){ public static String getNowTimeStr() {
return COMMON_FORMATTER_DATETIME.format(getNowTime()); return COMMON_FORMATTER_DATETIME.format(getNowTime());
} }
/** /**
* 判断日期格式是否合法 "yyyy-MM-dd" * 判断日期格式是否合法 "yyyy-MM-dd"
*
* @param strDate * @param strDate
* @return * @return
*/ */
public static boolean isValidDate(String strDate) { public static boolean isValidDate(String strDate) {
final int minLen = 10; final int minLen = 10;
if(strDate == null || strDate.length() < minLen) { if (strDate == null || strDate.length() < minLen) {
return false; return false;
} }
//正则表达式校验日期格式 yyyy-MM-dd //正则表达式校验日期格式 yyyy-MM-dd
String eL = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-" + String eL = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-" + "(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})" + "(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)(([0-9]{3}[1-9]|[0-9]{2}[1-9]" + "[0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-" + "(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|" + "((0[48]|[2468][048]|[3579][26])00))-02-29)";
"(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})" +
"(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)(([0-9]{3}[1-9]|[0-9]{2}[1-9]" +
"[0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-" +
"(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|" +
"((0[48]|[2468][048]|[3579][26])00))-02-29)";
Pattern pat = Pattern.compile(eL); Pattern pat = Pattern.compile(eL);
Matcher matcher = pat.matcher(strDate); Matcher matcher = pat.matcher(strDate);
return matcher.matches(); return matcher.matches();
@ -199,6 +231,7 @@ public class DateTimeUtil {
/** /**
* 判断时间格式 格式必须为 "YYYY-MM-DD HH:mm:ss" * 判断时间格式 格式必须为 "YYYY-MM-DD HH:mm:ss"
*
* @param sDateTime * @param sDateTime
* @return * @return
*/ */
@ -207,11 +240,7 @@ public class DateTimeUtil {
if ((sDateTime == null) || (sDateTime.length() < minLen)) { if ((sDateTime == null) || (sDateTime.length() < minLen)) {
return false; return false;
} }
String eL = "(((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13578]|1[02])-" + String eL = "(((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13578]|1[02])-" + "(0?[1-9]|[12]\\\\d|3[01]))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-" + "(0?[13456789]|1[012])-(0?[1-9]|[12]\\\\d|30))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-0?2-" + "(0?[1-9]|1\\\\d|2[0-8]))|(((1[6-9]|[2-9]\\\\d)(0[48]|[2468][048]|[13579][26])|((04|08|12|16|[2468][048]|" + "[3579][26])00))-0?2-29)) (20|21|22|23|[0-1]?\\\\d):[0-5]?\\\\d:[0-5]?\\\\d";
"(0?[1-9]|[12]\\\\d|3[01]))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-" +
"(0?[13456789]|1[012])-(0?[1-9]|[12]\\\\d|30))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-0?2-" +
"(0?[1-9]|1\\\\d|2[0-8]))|(((1[6-9]|[2-9]\\\\d)(0[48]|[2468][048]|[13579][26])|((04|08|12|16|[2468][048]|" +
"[3579][26])00))-0?2-29)) (20|21|22|23|[0-1]?\\\\d):[0-5]?\\\\d:[0-5]?\\\\d";
Pattern pat = Pattern.compile(eL); Pattern pat = Pattern.compile(eL);
Matcher matcher = pat.matcher(sDateTime); Matcher matcher = pat.matcher(sDateTime);
return matcher.matches(); return matcher.matches();