Java8 常用时间转换工具类
时间工具类
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class TimeUtil {
/**
* 北京时间
*/
private static final ZoneOffset BEIJING_ZONE = ZoneOffset.of("+8");
/**
* 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss
*/
private static final String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 标准日期格式:yyyy-MM-dd
*/
private static final String NORM_DATE_PATTERN = "yyyy-MM-dd";
/**
* 转换成时间戳
*
* @param localDateTime 当前日期时间
* @return 时间戳
*/
public static Long toTimestamp(LocalDateTime localDateTime) {
if (localDateTime == null) {
return null;
}
return localDateTime.toEpochSecond(BEIJING_ZONE);
}
/**
* 转换成时间戳
*
* @param localDate 当前日期
* @return 时间戳
*/
public static Long toTimestamp(LocalDate localDate) {
if (localDate == null) {
return null;
}
return localDate.atStartOfDay(BEIJING_ZONE).toEpochSecond();
}
/**
* 转换成Date
*
* @param localDateTime 当前日期时间
* @return Date日期
*/
public static Date toDate(LocalDateTime localDateTime) {
if (localDateTime == null) {
return null;
}
ZonedDateTime zonedDateTime = localDateTime.atZone(BEIJING_ZONE);
Instant instant = zonedDateTime.toInstant();
return Date.from(instant);
}
/**
* 转换成Date
*
* @param localDate 当前日期
* @return Date日期
*/
public static Date toDate(LocalDate localDate) {
if (localDate == null) {
return null;
}
ZonedDateTime zonedDateTime = localDate.atStartOfDay(BEIJING_ZONE);
Instant instant = zonedDateTime.toInstant();
return Date.from(instant);
}
/**
* 转换成时间字符串
*
* @param localDateTime 当前日期时间
* @return 时间字符串
*/
public static String toString(LocalDateTime localDateTime) {
if (localDateTime == null) {
return null;
}
return localDateTime.format(DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN));
}
/**
* 转换成时间字符串
*
* @param localDate 当前日期
* @return 时间字符串
*/
public static String toString(LocalDate localDate) {
if (localDate == null) {
return null;
}
return localDate.format(DateTimeFormatter.ofPattern(NORM_DATE_PATTERN));
}
/**
* 转换成日期时间
*
* @param text 时间字符串
* @return 日期时间
*/
public static LocalDateTime parseLocalDateTime(CharSequence text) {
if (text == null) {
return null;
}
return LocalDateTime.parse(text, DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN));
}
/**
* 转换成日期
*
* @param text 时间字符串
* @return 日期
*/
public static LocalDate parseLocalDate(CharSequence text) {
if (text == null) {
return null;
}
return LocalDate.parse(text, DateTimeFormatter.ofPattern(NORM_DATE_PATTERN));
}
/**
* 转换成日期时间
*
* @param timestamp 时间戳
* @return 日期时间
*/
public static LocalDateTime toLocalDateTime(Long timestamp) {
if (timestamp == null || timestamp == 0) {
return null;
}
return Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
}
/**
* 转换成日期
*
* @param timestamp 时间戳
* @return 日期
*/
public static LocalDate toLocalDate(Long timestamp) {
if (timestamp == null || timestamp == 0) {
return null;
}
return Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
}
public static void main(String[] args) {
System.out.println(TimeUtil.toTimestamp(LocalDateTime.now()));
System.out.println(TimeUtil.toTimestamp(LocalDate.now()));
System.out.println(TimeUtil.toDate(LocalDateTime.now()));
System.out.println(TimeUtil.toDate(LocalDate.now()));
System.out.println(TimeUtil.toString(LocalDateTime.now()));
System.out.println(TimeUtil.toString(LocalDate.now()));
System.out.println(TimeUtil.parseLocalDateTime("2021-04-12 11:01:02"));
System.out.println(TimeUtil.parseLocalDate("2021-04-12"));
}
}
输出结果
1618196882
1618156800
Mon Apr 12 11:08:02 CST 2021
Mon Apr 12 00:00:00 CST 2021
2021-04-12 11:08:02
2021-04-12
2021-04-12T11:01:02
2021-04-12