com.efeichong.util.DateUtils Maven / Gradle / Ivy
package com.efeichong.util;
import com.efeichong.exception.BaseException;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.collect.ImmutableMap;
import lombok.SneakyThrows;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author lxk
* @date 2021/1/4
* @description 时间处理工具类 基于joda
*/
public class DateUtils {
/**
* 默认加载的时间格式
*/
private static final Map patternMap = ImmutableMap.builder()
.put("\\d{4}-\\d{2}-\\d{2}", "yyyy-MM-dd")
.put("\\d{4}\\d{2}\\d{2}", "yyyyMMdd")
.put("\\d{4}/\\d{2}/\\d{2}", "yyyy/MM/dd")
.put("\\d{4}年\\d{2}月\\d{2}日", "yyyy年MM月dd日")
.put("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", "yyyy-MM-dd HH:mm:ss")
.put("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}", "yyyy/MM/dd HH:mm:ss")
.put("\\d{4}年\\d{2}月\\d{2}日 \\d{2}时\\d{2}分\\d{2}秒", "yyyy年MM月dd日 HH时mm分ss秒")
.build();
private static Cache cache = Caffeine.newBuilder()
.weakKeys()
.build();
/**
* 字符串转日期
*
* @param date
* @return
*/
@SneakyThrows
public static Date parse(String date) {
date = URLDecoder.decode(date, "utf-8");
String pattern = matcher(date);
if (pattern == null) {
throw new BaseException("未匹配到表达式");
}
DateTimeFormatter format = cache.get(pattern, formatter -> DateTimeFormat.forPattern(pattern));
return format.parseDateTime(date).toDate();
}
/**
* 字符串转日期
*
* @param date
* @return
*/
@SneakyThrows
public static Date parse(String date, String pattern) {
DateTimeFormatter format = cache.get(pattern, formatter -> DateTimeFormat.forPattern(pattern));
return format.parseDateTime(date).toDate();
}
/**
* 日期转字符串
*
* @param date 日期
* @param pattern 格式
* @return
*/
public static String format(Date date, String pattern) {
DateTimeFormatter format = cache.get(pattern, formatter -> DateTimeFormat.forPattern(pattern));
return format.print(date.getTime());
}
/**
* 日期转字符串
*
* @param mills 日期
* @param pattern 格式
* @return
*/
public static String format(Long mills, String pattern) {
if (mills == null) {
return "";
}
DateTimeFormatter format = cache.get(pattern, formatter -> DateTimeFormat.forPattern(pattern));
return format.print(mills);
}
/**
* 获取今天的0点
*
* @return
*/
public static Date getTodayZeroTime() {
return new DateTime().withTime(0, 0, 0, 0).toDate();
}
/**
* 两个日期间隔的天数
*
* @param start
* @param end
* @return
*/
public static int betweenDays(Date start, Date end) {
return Days.daysBetween(new DateTime(start), new DateTime(end)).getDays();
}
/**
* 两个日期间隔的天数
*
* @param start
* @param end
* @return
*/
public static int betweenDays(DateTime start, DateTime end) {
return Days.daysBetween(start, end).getDays();
}
/**
* 匹配字符串的日期格式
*
* @param date
* @return
*/
private static String matcher(String date) {
for (Map.Entry entry : patternMap.entrySet()) {
Pattern pattern = Pattern.compile(entry.getKey());
Matcher match = pattern.matcher(date);
if (match.matches()) {
return entry.getValue();
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy