package jnpf.base.util;
|
|
import cn.hutool.core.date.LocalDateTimeUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
import jnpf.util.DateUtil;
|
|
import java.sql.Timestamp;
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.Date;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* 时间格式 常量
|
*/
|
public class DateTimeFormatConstant {
|
DateTimeFormatConstant() {
|
}
|
|
public static final String YEAR = "yyyy";
|
public static final String YEAR_MONTH = "yyyy-MM";
|
public static final String YEAR_MONTH_DATE = "yyyy-MM-dd";
|
public static final String YEAR_MONTH_DHM = "yyyy-MM-dd HH:mm";
|
public static final String YEAR_MONTH_DHMS = "yyyy-MM-dd HH:mm:ss";
|
public static final String HOUR_MINUTE = "HH:mm";
|
public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";
|
private static final Pattern YEAR_PATTERN = Pattern.compile("^\\d{4}$");
|
private static final Pattern YEAR_MONTH_PATTERN = Pattern.compile("^(\\d{4})-(\\d{1,2})$");
|
private static final Pattern YEAR_MONTH_DATE_PATTERN = Pattern.compile("^(\\d{4})-(\\d{1,2})-(\\d{1,2})$");
|
|
/**
|
* 时间格式忽略大小写
|
*
|
* @param otherFormat
|
* @return 返回固定格式
|
*/
|
public static String getFormat(String otherFormat) {
|
if (YEAR.equalsIgnoreCase(otherFormat)) {
|
return YEAR;
|
}
|
if (YEAR_MONTH.equalsIgnoreCase(otherFormat)) {
|
return YEAR_MONTH;
|
}
|
if (YEAR_MONTH_DATE.equalsIgnoreCase(otherFormat)) {
|
return YEAR_MONTH_DATE;
|
}
|
if (YEAR_MONTH_DHM.equalsIgnoreCase(otherFormat)) {
|
return YEAR_MONTH_DHM;
|
}
|
if (YEAR_MONTH_DHMS.equalsIgnoreCase(otherFormat)) {
|
return YEAR_MONTH_DHMS;
|
}
|
if (HOUR_MINUTE.equalsIgnoreCase(otherFormat)) {
|
return HOUR_MINUTE;
|
}
|
if (HOUR_MINUTE_SECOND.equalsIgnoreCase(otherFormat)) {
|
return HOUR_MINUTE_SECOND;
|
}
|
return otherFormat;
|
}
|
|
/**
|
* 数据库查询时间字段-转换成long
|
* 不同数据库查询结果的对象不同
|
*
|
* @param dateObj
|
* @return long
|
*/
|
public static Long getDateObjToLong(Object dateObj) {
|
LocalDateTime dateTime = null;
|
if (ObjectUtil.isNotEmpty(dateObj)) {
|
if (dateObj instanceof LocalDateTime) {
|
dateTime = (LocalDateTime) dateObj;
|
} else if (dateObj instanceof Timestamp) {
|
dateTime = ((Timestamp) dateObj).toLocalDateTime();
|
} else if (dateObj instanceof Long) {
|
dateTime = LocalDateTimeUtil.of(new Date(Long.parseLong(dateObj.toString())));
|
} else {
|
dateTime = parseDateText(dateObj.toString());
|
}
|
}
|
return dateTime != null ? DateUtil.localDateTime2Millis(dateTime) : null;
|
}
|
|
public static boolean isYearMonthText(Object value) {
|
return normalizeYearMonthText(value) != null;
|
}
|
|
public static String normalizeYearMonthText(Object value) {
|
if (!(value instanceof String)) {
|
return null;
|
}
|
String dateText = ((String) value).trim().replace("/", "-");
|
Matcher yearMonthMatcher = YEAR_MONTH_PATTERN.matcher(dateText);
|
if (!yearMonthMatcher.matches()) {
|
return null;
|
}
|
return yearMonthMatcher.group(1) + "-" + padDatePart(yearMonthMatcher.group(2));
|
}
|
|
private static LocalDateTime parseDateText(String value) {
|
String dateText = value == null ? "" : value.trim().replace("/", "-");
|
if (dateText.isEmpty()) {
|
return null;
|
}
|
if (YEAR_PATTERN.matcher(dateText).matches()) {
|
return LocalDateTime.parse(dateText + "-01-01 00:00:00", DateTimeFormatter.ofPattern(YEAR_MONTH_DHMS));
|
}
|
String yearMonthText = normalizeYearMonthText(dateText);
|
if (yearMonthText != null) {
|
return LocalDateTime.parse(yearMonthText + "-01 00:00:00", DateTimeFormatter.ofPattern(YEAR_MONTH_DHMS));
|
}
|
Matcher yearMonthDateMatcher = YEAR_MONTH_DATE_PATTERN.matcher(dateText);
|
if (yearMonthDateMatcher.matches()) {
|
return LocalDateTime.parse(yearMonthDateMatcher.group(1) + "-" + padDatePart(yearMonthDateMatcher.group(2)) + "-" + padDatePart(yearMonthDateMatcher.group(3)) + " 00:00:00", DateTimeFormatter.ofPattern(YEAR_MONTH_DHMS));
|
}
|
return LocalDateTimeUtil.of(cn.hutool.core.date.DateUtil.parse(dateText));
|
}
|
|
private static String padDatePart(String value) {
|
return String.format("%02d", Integer.parseInt(value));
|
}
|
|
}
|