Java自用日期时间转换工具类

由于在工作用遇到数据库存储格式为 LocalDateTime 格式的日期时间,但前端传的为 String 类型的日期时间,需要转换类型,故自整理此工具类,后续有新功能再进行完善。

package com.domain.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
* 日期时间转换工具
*/
public abstract class DateTimeUtils {

private DateTimeUtils() {
}

// 常量Formatter Date格式
private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd");

// 常量Formatter DateTime格式
private static final DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

/**
* 字符串转LocalDateTime
*
* @param str "yyyy-MM-dd HH:mm:ss"格式时间字符串
* @return LocalDateTime
*/
public static LocalDateTime strToLocalDateTime(String str) {
try {
return LocalDateTime.parse(str, DATE_TIME);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("日期时间转换工具异常");
}
}

/**
* 字符串转LocalDate
*
* @param str "yyyy-MM-dd"格式时间字符串
* @return LocalDate
*/
public static LocalDate strToLocalDate(String str) {
try {
return LocalDate.parse(str, DATE);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("日期时间转换工具异常");
}
}

/**
* LocalDateTime转字符串
*
* @param localDateTime LocalDateTime
* @return "yyyy-MM-dd HH:mm:ss"格式时间字符串
*/
public static String localDateTimeToStr(LocalDateTime localDateTime) {
try {
return localDateTime.format(DATE_TIME);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("日期时间转换工具异常");
}
}

/**
* LocalDate转字符串
*
* @param localDate LocalDate
* @return "yyyy-MM-dd"格式时间字符串
*/
public static String localDateToStr(LocalDate localDate) {
try {
return localDate.format(DATE);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("日期时间转换工具异常");
}
}

/**
* LocalDateTime转Date
*
* @param localDateTime 默认时区日期时间
* @return Data
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
try {
// 系统默认时区
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("日期时间转换工具异常");
}
}

/**
* LocalDateTime转Date
*
* @param localDateTime 默认时区日期时间
* @param hours 时区数字
* @return Data
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime, Integer hours) {
try {
// 根据传值选择时区
return Date.from(localDateTime.toInstant(ZoneOffset.ofHours(hours)));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("日期时间转换工具异常");
}
}

/**
* LocalDate转Date
*
* @param localDate 默认时区日期
* @return Data
*/
public static Date localDateToDate(LocalDate localDate) {
try {
// 系统默认时区
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("日期时间转换工具异常");
}
}

/**
* LocalDate转Date
*
* @param localDate 默认时区日期
* @param hours 时区数字
* @return Data
*/
public static Date localDateToDate(LocalDate localDate, Integer hours) {
try {
// 根据传值选择时区
return Date.from(localDate.atStartOfDay().toInstant(ZoneOffset.ofHours(hours)));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("日期时间转换工具异常");
}
}
}