在java开发过程中,往往需要日期处理,java官方推荐使用java.time包下面的线程安全的日期处理库。现基于java.time包实现的线程安全的日期格式处理工具类如下:
import org.apache.commons.lang3.StringUtils;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
/**
* @description: 线程安全的日期处理
*
* @author pony
* @date 2023/6/19 20:49
* @version 1.0
*/
public class ConcurrentDateUtils {
/**
* 获取当前时间戳,精确到毫秒
* @return Long
*/
public static Long getCurrentTimeMillis() {
return Instant.now().toEpochMilli();
}
/**
* 获取当前时间戳,精确到秒
* @return Long
*/
public static Long getCurrentSecond() {
return Instant.now().getEpochSecond();
}
/**
* 获取当前时间串
* @param formatPattern
* @return String
*/
public static String getCurrentDateStringByFormat(FormatPattern formatPattern) {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(formatPattern.getS()));
}
/**
* 将Long类型的时间戳,按指定格式转成字符串
* @param ts
* @param formatPattern
* @return String
*/
public static String getDateStringByTimeStampAndFormat(Long ts, FormatPattern formatPattern) {
return ts != null ? DateTimeFormatter.ofPattern(formatPattern.getS()).format(LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.systemDefault())) : null;
}
/**
* 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss|yyyy-MM-dd HH:mm:ss.SSS
* @param dateString
* @param formatPattern
* @return Long
*/
public static Long getTimeStampByDateString(String dateString, FormatPattern formatPattern) {
LocalDateTime parse = LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(formatPattern.getS()));
return StringUtils.isNotBlank(dateString) ? LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() : -1L;
}
/** LocalDateTime转时间戳 */
public static Long localDate2Second(LocalDateTime localDateTime) {
return localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
// return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
}
/**
* 取本月第一天
*/
public static LocalDate firstDayOfThisMonth() {
LocalDate today = LocalDate.now();
return today.with(TemporalAdjusters.firstDayOfMonth());
}
/**
* 取下月第一天
*/
public static LocalDate firstDayOfLastMonth() {
LocalDate today = LocalDate.now();
today = today.minusMonths(1);
return today.with(TemporalAdjusters.firstDayOfMonth());
}
/**
* 取本月第N天
*/
public static LocalDate dayOfThisMonth(int n) {
LocalDate today = LocalDate.now();
return today.withDayOfMonth(n);
}
/**
* 取本月最后一天
*/
public static LocalDate lastDayOfThisMonth() {
LocalDate today = LocalDate.now();
return today.with(TemporalAdjusters.lastDayOfMonth());
}
/**
* 获取本周一
*/
public static LocalDateTime firstDayOfWeek(Long date) {
// long转LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
return localDateTime.with(DayOfWeek.MONDAY);
}
/**
* 获取上周一
*/
public static LocalDateTime firstDayOfLastWeek(Long date) {
// long转LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
LocalDateTime localDateTime1 = localDateTime.plusDays(-7);
return localDateTime1.with(DayOfWeek.MONDAY);
}
/**
* 获取上周一
*/
public static LocalDateTime lastDay(Long date) {
// long转LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(date), ZoneId.systemDefault());
LocalDateTime localDateTime1 = localDateTime.plusDays(-1);
return localDateTime1;
}
/**
* 取本月第一天的开始时间
*/
public static LocalDateTime startOfThisMonth() {
return LocalDateTime.of(firstDayOfThisMonth(), LocalTime.MIN);
}
/**
* 取本月最后一天的结束时间
*/
public static LocalDateTime endOfThisMonth() {
return LocalDateTime.of(lastDayOfThisMonth(), LocalTime.MAX);
}
public enum FormatPattern {
DATE_PATTERN("yyyy-MM-dd"),
TIME_PATTERN("HH:mm:ss"),
DATE_TIME_PATTERN("yyyy-MM-dd HH:mm:ss"),
DATE_TIME_SSS_PATTERN("yyyy-MM-dd HH:mm:ss.SSS");
private String s;
FormatPattern(String s) {
this.s = s;
}
public String getS() {
return s;
}
}
}