import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
/**
* java 8 日期工具类
*
* @author huangqh
*
*/
public class DateUtils {
/**
* 日期正则表达式(不支持闰年判断),可以匹配以下日期格式
* yyyy-MM-dd, yyyy/MM/dd, yyyyMMdd
*/
public static final String ALL_DATE_REGEX = "^((19|20)\\d{2}-(0[1-9]|1[0-2])-(0[1-9]|1\\d|2\\d|3[0-1]))"
+ "|((19|20)\\d{2}/(0[1-9]|1[0-2])/(0[1-9]|1\\d|2\\d|3[0-1]))"
+ "|((19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|1\\d|2\\d|3[0-1]))$";
/**
*
* 时间戳日期正则表达式(不支持闰年判断),可以匹配以下日期格式
* yyyy-MM-dd HH:mm:ss, yyyyMMddHHmmss
*/
public static final String ALL_DATE_TIME_REGEX = "^"
+ "|((19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|1\\d|2\\d|3[0-1])(2[0-3]|[0-1]\\d)[0-5]\\d[0-5]\\d)"
+ "|((19|20)\\d{2}-(0[1-9]|1[0-2])-(0[1-9]|1\\d|2\\d|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d)"
+ "|((19|20)\\d{2}/(0[1-9]|1[0-2])/(0[1-9]|1\\d|2\\d|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d)$";
/**
* 日期格式yyyy-MM-dd正则表达式
*/
public static final String DATE_REGEX = "^(19|20)\\d{2}-(0[1-9]|1[0-2])-(0[1-9]|1\\d|2\\d|3[0-1])$";
/**
* 日期格式yyyy/MM/dd正则表达式
*/
public static final String DATE_REGEX_ = "^(19|20)\\d{2}/(0[1-9]|1[0-2])/(0[1-9]|1\\d|2\\d|3[0-1])$";
/**
* 日期格式yyyyMMdd正则表达式
*/
public static final String DATEREGEX = "^(19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|1\\d|2\\d|3[0-1])$";
/**
* 时间格式HH:mm:ss正则表达式
*/
public static final String TIMEREGEX = "^(2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d$";
/**
* 时间戳yyyyMMddHHmmss正则表达式
*/
public static final String TIMESTAMPREGEX = "^(19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|1\\d|2\\d|3[0-1])(2[0-3]|[0-1]\\d)[0-5]\\d[0-5]\\d$";
/**
* 时间戳yyyy-MM-dd HH:mm:ss正则表达式
*/
public static final String TIMESTAMP_REGEX = "^(19|20)\\d{2}-(0[1-9]|1[0-2])-(0[1-9]|1\\d|2\\d|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d$";
/**
* 时间戳yyyy/MM/dd HH:mm:ss正则表达式
*/
public static final String TIMESTAMP_REGEX_ = "^(19|20)\\d{2}/(0[1-9]|1[0-2])/(0[1-9]|1\\d|2\\d|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d$";
/**
* 日期格式yyyy-MM-dd
*/
public static String DATE_PATTERN = "yyyy-MM-dd";
/**
* 日期格式yyyy/MM/dd
*/
public static String DATE_PATTERN_ = "yyyy/MM/dd";
/**
* 日期格式yyyyMMdd
*/
public static String DATEPATTERN = "yyyyMMdd";
/**
* 时间格式HH:mm:ss
*/
public static String TIMEPATTERN = "HH:mm:ss";
/**
* 日期时间格式yyyy-MM-dd HH:mm:ss
*/
public static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 日期时间格式yyyy/MM/dd HH:mm:ss
*/
public static String DATE_TIME_PATTERN_ = "yyyy/MM/dd HH:mm:ss";
/**
* 日期时间格式yyyyMMddHHmmss
*/
public static String DATETIMEPATTERN = "yyyyMMddHHmmss";
/**
* 日期判断(不支持闰年判断),可以匹配以下日期格式
* yyyy-MM-dd, yyyy/MM/dd, yyyyMMdd
* @param date
* @return
*/
public static boolean isDate(String date) {
if (date.isEmpty()) {
return false;
}
return date.matches(ALL_DATE_REGEX);
}
/**
* 时间戳日期正则表达式(不支持闰年判断),可以匹配以下日期格式
* yyyy-MM-dd HH:mm:ss, yyyyMMddHHmmss
* @param date
* @return
*/
public static boolean isDateTime(String date) {
if (date.isEmpty()) {
return false;
}
return date.matches(ALL_DATE_TIME_REGEX);
}
/**
* LocalDateTime转换为java.util.Date
*
* @param dateTime
* @return
*/
public static Date localDateTimeToDate(LocalDateTime dateTime) {
return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* LocalDate转java.util.Date
*
* @param localDate
* @return
*/
public static Date localDateToDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
/**
* java.util.Date转LocalDateTime
*
* @param date
* @return
*/
public static LocalDateTime dateToLocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
/**
* 将long类型的timestamp转为LocalDateTime
*
* @param timestamp
* @return LocalDateTime
*/
public static LocalDateTime longTimestampToDateTime(long timestamp) {
Instant instant = Instant.ofEpochMilli(timestamp);
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
/**
* 把String日期转为LocalDateTime
* @param time
* @return
*/
public static LocalDateTime stringToLocalDateTime(String time) {
if (time.isEmpty()) {
return null;
}
// yyyyMMdd或yyyy-mm-dd或yyyy/mm/dd
if (isDate(time)) {
return stringToLocalDate(time).atStartOfDay();
}
// yyyyMMddHHmmss
if (time.matches(TIMESTAMPREGEX)) {
return stringToLocalDateTime(time, DATETIMEPATTERN);
}
// yyyy-MM-dd HH:mm:ss
if (time.matches(TIMESTAMP_REGEX)) {
return stringToLocalDateTime(time, DATE_TIME_PATTERN);
}
// yyyy/MM/dd HH:mm:ss
if (time.matches(TIMESTAMP_REGEX_)) {
return stringToLocalDateTime(time, DATE_TIME_PATTERN_);
}
// 其它返回空
return null;
}
/**
* 将String日期按格式转为LocalDateTime
*
* @param time
* @param format
* @return
*/
public static LocalDateTime stringToLocalDateTime(String time, String format) {
if (time.isEmpty() || format.isEmpty()) {
return null;
}
return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(format));
}
/**
* java.util.Date转LocalDate
*
* @param date
* @return
*/
public static LocalDate dateToLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* 将String日期转为LocalDate
* @param time
* @return
*/
public static LocalDate stringToLocalDate(String date) {
if (date.isEmpty()) {
return null;
}
// yyyy-MM-dd
if (date.matches(DATE_REGEX)) {
return stringToLocalDate(date, DATE_PATTERN);
}
// yyyy/MM/dd
if (date.matches(DATE_REGEX_)) {
return stringToLocalDate(date, DATE_PATTERN_);
}
// yyyyMMdd
if (date.matches(DATEREGEX)) {
return stringToLocalDate(date, DATEPATTERN);
}
// yyyyMMddHHmmss
if (date.matches(TIMESTAMPREGEX)) {
return stringToLocalDate(date, DATETIMEPATTERN);
}
// yyyy-MM-dd HH:mm:ss
if (date.matches(TIMESTAMP_REGEX)) {
return stringToLocalDate(date, DATE_TIME_PATTERN);
}
// yyyy/MM/dd HH:mm:ss
if (date.matches(TIMESTAMP_REGEX_)) {
return stringToLocalDate(date, DATE_TIME_PATTERN_);
}
// 其它返回空
return null;
}
/**
* 将String日期按格式转为LocalDate
*
* @param time
* @param format
* @return
*/
public static LocalDate stringToLocalDate(String date, String format) {
if (date.isEmpty() || format.isEmpty()) {
return null;
}
return LocalDate.parse(date, DateTimeFormatter.ofPattern(format));
}
/**
* LocalDateTime转String,按pattern格式化
*
* @param dateTime
* @param pattern
* @return
*/
public static String formatDateTime(LocalDateTime dateTime, String pattern) {
if (dateTime == null || pattern.isEmpty()) {
return null;
}
return dateTime.format(DateTimeFormatter.ofPattern(pattern));
}
/**
* 将LocalDateTime转为String yyyy-MM-dd HH:mm:ss格式
*
* @param dateTime
* @return
*/
public static String formatDateTime(LocalDateTime dateTime) {
return formatDateTime(dateTime, DATE_TIME_PATTERN);
}
/**
* 将LocalDateTime转为long类型的timestamp
*
* @param localDateTime
* @return
*/
public static long dateTimeToLongTimestamp(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return instant.toEpochMilli();
}
/**
* 将LocalDate转为long类型的timestamp
*
* @param localDate
* @return
*/
public static long localDateToLongTimestamp(LocalDate localDate) {
return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* 将String转成Timestamp
*/
public static Timestamp stringToTimestamp(String datetime) {
// 空返回空
if (datetime.isEmpty()) {
return null;
}
// localDate
if (isDate(datetime)) {
return new Timestamp(localDateToLongTimestamp(stringToLocalDate(datetime)));
}
// localDatetime
if (isDateTime(datetime)) {
return new Timestamp(dateTimeToLongTimestamp(stringToLocalDateTime(datetime)));
}
// 其它返回空
return null;
}
/**
* 将String转为Date
* @param date
* @return
*/
public static Date stringToDate(String date) {
// 空返回空
if (date.isEmpty()) {
return null;
}
// localDate
if (isDate(date)) {
return localDateToDate(stringToLocalDate(date));
}
// localDatetime
if (isDateTime(date)) {
return localDateTimeToDate(stringToLocalDateTime(date));
}
// 其它返回空
return null;
}
/**
* 获取今天的00:00:00
*
* @return
*/
public static String getDayStart() {
return getDayStart(LocalDateTime.now());
}
/**
* 获取某天的00:00:00
*
* @param dateTime
* @return
*/
public static String getDayStart(LocalDateTime dateTime) {
return formatDateTime(dateTime.with(LocalTime.MIN));
}
/**
* 获取今天的23:59:59
*
* @return
*/
public static String getDayEnd() {
return getDayEnd(LocalDateTime.now());
}
/**
* 获取某天的23:59:59
*
* @param dateTime
* @return
*/
public static String getDayEnd(LocalDateTime dateTime) {
return formatDateTime(dateTime.with(LocalTime.MAX));
}
/**
* 获取本月第一天的00:00:00
*
* @return
*/
public static String getFirstDayOfMonth() {
return getFirstDayOfMonth(LocalDateTime.now());
}
/**
* 获取本月最后一天的23:59:59
*
* @return
*/
public static String getLastDayOfMonth() {
return getLastDayOfMonth(LocalDateTime.now());
}
/**
* 获取某月第一天的00:00:00
*
* @param dateTime
* @return
*/
public static String getFirstDayOfMonth(LocalDateTime dateTime) {
return formatDateTime(dateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN));
}
/**
* 获取某月最后一天的23:59:59
*
* @param dateTime
* @return
*/
public static String getLastDayOfMonth(LocalDateTime dateTime) {
return formatDateTime(dateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX));
}
/**
* 获取N年前或后的日期
* @param n
* @return
*/
public static LocalDateTime getNumberYears(int n) {
LocalDateTime now = LocalDateTime.now();
if (n < 0) return now.minusYears(-n);
if (n > 0) return now.plusYears(n);
return now;
}
/**
* 获取N个月前或后的日期
* @param n
* @return
*/
public static LocalDateTime getNumberMonths(int n) {
LocalDateTime now = LocalDateTime.now();
if (n < 0) return now.minusMonths(-n);
if (n > 0) return now.plusMonths(n);
return now;
}
/**
* 获取N日前或后的日期
* @param n
* @return
*/
public static LocalDateTime getNumberDays(int n) {
LocalDateTime now = LocalDateTime.now();
if (n < 0) return now.minusDays(-n);
if (n > 0) return now.plusDays(n);
return now;
}
/**
* 获取N周前或后的日期
* @param n
* @return
*/
public static LocalDateTime getNumberWeeks(int n) {
LocalDateTime now = LocalDateTime.now();
if (n < 0) return now.minusWeeks(-n);
if (n > 0) return now.plusWeeks(n);
return now;
}
/**
* 获取N小时前或后的日期
* @param n
* @return
*/
public static LocalDateTime getNumberHours(int n) {
LocalDateTime now = LocalDateTime.now();
if (n < 0) return now.minusHours(-n);
if (n > 0) return now.plusHours(n);
return now;
}
/**
* 获取N分钟前或后的日期
* @param n
* @return
*/
public static LocalDateTime getNumberMinutes(int n) {
LocalDateTime now = LocalDateTime.now();
if (n < 0) return now.minusMinutes(-n);
if (n > 0) return now.plusMinutes(n);
return now;
}
/**
* 获取N秒前或后的日期
* @param n
* @return
*/
public static LocalDateTime getNumberSeconds(int n) {
LocalDateTime now = LocalDateTime.now();
if (n < 0) return now.minusSeconds(-n);
if (n > 0) return now.plusSeconds(n);
return now;
}
/**
* 两个日期相减得到的天数
* @param beginDate
* @param endDate
* @return
*/
public static int getDiffDays(Date beginDate, Date endDate) {
if (beginDate == null || endDate == null) {
throw new IllegalArgumentException("getDiffDays param is null!");
}
long diff = (endDate.getTime() - beginDate.getTime())/(1000*60*60*24);
int days = new Long(diff).intValue();
return days;
}
}
java 8 日期工具类
最新推荐文章于 2024-04-18 14:56:31 发布