优化时间转换工具类
This commit is contained in:
parent
2cb11615cd
commit
6593241be0
@ -1,12 +1,16 @@
|
||||
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.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 日期时间工具
|
||||
*
|
||||
* @author tajochen
|
||||
*/
|
||||
public class DateTimeUtil {
|
||||
@ -19,16 +23,26 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 字符串转LocalDate
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate stringToDate(String date) {
|
||||
assert date != null;
|
||||
return LocalDate.parse(date, COMMON_FORMATTER_DATE);
|
||||
LocalDate parse = null;
|
||||
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转字符串
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
@ -39,6 +53,7 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* LocalDateTime转字符串
|
||||
*
|
||||
* @param dateTime
|
||||
* @return
|
||||
*/
|
||||
@ -49,16 +64,26 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 字符串转LocalDateTime
|
||||
* @param dateStr
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime stringToDateTime(String dateStr) {
|
||||
assert dateStr != null;
|
||||
return LocalDateTime.parse(dateStr, COMMON_FORMATTER_DATETIME);
|
||||
public static LocalDateTime stringToDateTime(String date) {
|
||||
LocalDateTime parse = null;
|
||||
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时间戳
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
@ -69,6 +94,7 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* LocalDateTime转字符串(格式化)
|
||||
*
|
||||
* @param dateTime
|
||||
* @param formatter
|
||||
* @return
|
||||
@ -80,6 +106,7 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 字符串转LocalDateTime(格式化)
|
||||
*
|
||||
* @param dateStr
|
||||
* @param formatter
|
||||
* @return
|
||||
@ -91,10 +118,11 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 字符串转 local date
|
||||
*
|
||||
* @param dateStr
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate stringToDateFormatter(String dateStr){
|
||||
public static LocalDate stringToDateFormatter(String dateStr) {
|
||||
LocalDate date = LocalDate.parse(dateStr, COMMON_FORMATTER_DATE);
|
||||
return date;
|
||||
}
|
||||
@ -102,6 +130,7 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 日期转时间戳
|
||||
*
|
||||
* @param dateTime
|
||||
* @return
|
||||
*/
|
||||
@ -112,6 +141,7 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 时间戳转日期
|
||||
*
|
||||
* @param timeMillis
|
||||
* @return
|
||||
*/
|
||||
@ -122,6 +152,7 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 时间戳转时间
|
||||
*
|
||||
* @param timeMillis
|
||||
* @return
|
||||
*/
|
||||
@ -132,16 +163,18 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 时间戳转时间再转字符串
|
||||
*
|
||||
* @param timeMillis
|
||||
* @return
|
||||
*/
|
||||
public static String timeMillisToString(long timeMillis){
|
||||
public static String timeMillisToString(long timeMillis) {
|
||||
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeMillis, 0, ZoneOffset.ofHours(8));
|
||||
return COMMON_FORMATTER_DATETIME.format(localDateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间 hh:mm:ss:nnn
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime getNowTime() {
|
||||
@ -151,6 +184,7 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 获取当前日期 yyyy-MM-dd
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getToday() {
|
||||
@ -160,6 +194,7 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 获取当前 Instant 时间戳
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Instant getInstant() {
|
||||
@ -169,29 +204,26 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 获取当前时间 yyyy-MM-ss hh:mm:ss
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getNowTimeStr(){
|
||||
public static String getNowTimeStr() {
|
||||
return COMMON_FORMATTER_DATETIME.format(getNowTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断日期格式是否合法 "yyyy-MM-dd"
|
||||
*
|
||||
* @param strDate
|
||||
* @return
|
||||
*/
|
||||
public static boolean isValidDate(String strDate) {
|
||||
final int minLen = 10;
|
||||
if(strDate == null || strDate.length() < minLen) {
|
||||
if (strDate == null || strDate.length() < minLen) {
|
||||
return false;
|
||||
}
|
||||
//正则表达式校验日期格式 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])-" +
|
||||
"(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)";
|
||||
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)";
|
||||
Pattern pat = Pattern.compile(eL);
|
||||
Matcher matcher = pat.matcher(strDate);
|
||||
return matcher.matches();
|
||||
@ -199,6 +231,7 @@ public class DateTimeUtil {
|
||||
|
||||
/**
|
||||
* 判断时间格式 格式必须为 "YYYY-MM-DD HH:mm:ss"
|
||||
*
|
||||
* @param sDateTime
|
||||
* @return
|
||||
*/
|
||||
@ -207,11 +240,7 @@ public class DateTimeUtil {
|
||||
if ((sDateTime == null) || (sDateTime.length() < minLen)) {
|
||||
return false;
|
||||
}
|
||||
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";
|
||||
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";
|
||||
Pattern pat = Pattern.compile(eL);
|
||||
Matcher matcher = pat.matcher(sDateTime);
|
||||
return matcher.matches();
|
||||
|
Loading…
Reference in New Issue
Block a user