packagecom.xkzhangsan.time.converter;importjava.time.Instant;importjava.time.LocalDate;importjava.time.LocalDateTime;importjava.time.LocalTime;importjava.time.ZoneId;importjava.time.ZonedDateTime;importjava.time.temporal.TemporalAccessor;importjava.util.Date;importjava.util.Objects;/*** 日期转换
* 包含Date、LocalDate、LocalDateTime、LocalTime、Instant和ZonedDateTime的互相转换
*
* 注意,ZonedDateTime相关的转换,尤其是其他时间转ZonedDateTime,要注意时间和对应时区一致。
* @ClassName: DateTimeConverterUtil
* @Description: DateTime Converter
*@authorxkzhangsan
* @date 2019年12月1日
**/
public classDateTimeConverterUtil {privateDateTimeConverterUtil(){
}/*** LocalDateTime转Date
*@paramlocalDateTime
*@return
*/
public staticDate toDate(LocalDateTime localDateTime) {
Objects.requireNonNull(localDateTime,"localDateTime");returnDate.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}/*** LocalDate转Date
*@paramlocalDate
*@return
*/
public staticDate toDate(LocalDate localDate) {
Objects.requireNonNull(localDate,"localDate");returnDate.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
}/*** LocalTime转Date
* 以当天的日期+LocalTime组成新的LocalDateTime转换为Date
*@paramlocalTime
*@return
*/
public staticDate toDate(LocalTime localTime) {
Objects.requireNonNull(localTime,"localTime");returnDate.from(LocalDate.now().atTime(localTime).atZone(ZoneId.systemDefault()).toInstant());
}/*** Instant转Date
*@paraminstant
*@return
*/
public staticDate toDate(Instant instant) {returnDate.from(instant);
}/*** epochMilli毫秒转Date
*@paramepochMilli
*@return
*/
public static Date toDate(longepochMilli){
Objects.requireNonNull(epochMilli,"epochMilli");return newDate(epochMilli);
}/*** ZonedDateTime转Date
* 注意时间对应的时区和默认时区差异
*@paramzonedDateTime
*@return
*/
public staticDate toDate(ZonedDateTime zonedDateTime) {
Objects.requireNonNull(zonedDateTime,"zonedDateTime");returnDate.from(zonedDateTime.toInstant());
}/*** Date转LocalDateTime
*@paramdate
*@return
*/
public staticLocalDateTime toLocalDateTime(Date date) {
Objects.requireNonNull(date,"date");returnInstant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
}/*** LocalDate转LocalDateTime
*@paramlocalDate
*@return
*/
public staticLocalDateTime toLocalDateTime(LocalDate localDate) {
Objects.requireNonNull(localDate,"localDate");returnlocalDate.atStartOfDay();
}/*** LocalTime转LocalDateTime
* 以当天的日期+LocalTime组成新的LocalDateTime
*@paramlocalTime
*@return
*/
public staticLocalDateTime toLocalDateTime(LocalTime localTime) {
Objects.requireNonNull(localTime,"localTime");returnLocalDate.now().atTime(localTime);
}/*** Instant转LocalDateTime
*@paraminstant
*@return
*/
public staticLocalDateTime toLocalDateTime(Instant instant) {returnLocalDateTime.ofInstant(instant, ZoneId.systemDefault());
}/*** epochMilli毫秒转LocalDateTime
*@paramepochMilli
*@return
*/
public static LocalDateTime toLocalDateTime(longepochMilli) {
Objects.requireNonNull(epochMilli,"epochMilli");returnLocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
}/*** temporal转LocalDateTime
*@paramtemporal
*@return
*/
public staticLocalDateTime toLocalDateTime(TemporalAccessor temporal) {returnLocalDateTime.from(temporal);
}/*** ZonedDateTime转LocalDateTime
* 注意时间对应的时区和默认时区差异
*@paramzonedDateTime
*@return
*/
public staticLocalDateTime toLocalDateTime(ZonedDateTime zonedDateTime) {
Objects.requireNonNull(zonedDateTime,"zonedDateTime");returnzonedDateTime.toLocalDateTime();
}/*** Date转LocalDate
*@paramdate
*@return
*/
public staticLocalDate toLocalDate(Date date) {returntoLocalDateTime(date).toLocalDate();
}/*** LocalDateTime转LocalDate
*@paramlocalDateTime
*@return
*/
public staticLocalDate toLocalDate(LocalDateTime localDateTime) {
Objects.requireNonNull(localDateTime,"localDateTime");returnlocalDateTime.toLocalDate();
}/*** Instant转LocalDate
*@paraminstant
*@return
*/
public staticLocalDate toLocalDate(Instant instant) {returntoLocalDateTime(instant).toLocalDate();
}/*** temporal转LocalDate
*@paramtemporal
*@return
*/
public staticLocalDate toLocalDate(TemporalAccessor temporal) {returnLocalDate.from(temporal);
}/*** ZonedDateTime转LocalDate
* 注意时间对应的时区和默认时区差异
*@paramzonedDateTime
*@return
*/
public staticLocalDate toLocalDate(ZonedDateTime zonedDateTime) {
Objects.requireNonNull(zonedDateTime,"zonedDateTime");returnzonedDateTime.toLocalDate();
}/*** Date转LocalTime
*@paramdate
*@return
*/
public staticLocalTime toLocalTime(Date date) {returntoLocalDateTime(date).toLocalTime();
}/*** LocalDateTime转LocalTime
*@paramlocalDateTime
*@return
*/
public staticLocalTime toLocalTime(LocalDateTime localDateTime) {
Objects.requireNonNull(localDateTime,"localDateTime");returnlocalDateTime.toLocalTime();
}/*** Instant转LocalTime
*@paraminstant
*@return
*/
public staticLocalTime toLocalTime(Instant instant) {returntoLocalDateTime(instant).toLocalTime();
}/*** temporal转LocalTime
*@paramtemporal
*@return
*/
public staticLocalTime toLocalTime(TemporalAccessor temporal) {returnLocalTime.from(temporal);
}/*** ZonedDateTime转LocalTime
* 注意时间对应的时区和默认时区差异
*@paramzonedDateTime
*@return
*/
public staticLocalTime toLocalTime(ZonedDateTime zonedDateTime) {
Objects.requireNonNull(zonedDateTime,"zonedDateTime");returnzonedDateTime.toLocalTime();
}/*** Date转Instant
*@paramdate
*@return
*/
public staticInstant toInstant(Date date) {
Objects.requireNonNull(date,"date");returndate.toInstant();
}/*** LocalDateTime转Instant
*@paramlocalDateTime
*@return
*/
public staticInstant toInstant(LocalDateTime localDateTime) {
Objects.requireNonNull(localDateTime,"localDateTime");returnlocalDateTime.atZone(ZoneId.systemDefault()).toInstant();
}/*** LocalDate转Instant
*@paramlocalDate
*@return
*/
public staticInstant toInstant(LocalDate localDate) {returntoLocalDateTime(localDate).atZone(ZoneId.systemDefault()).toInstant();
}/*** LocalTime转Instant
* 以当天的日期+LocalTime组成新的LocalDateTime转换为Instant
*@paramlocalTime
*@return
*/
public staticInstant toInstant(LocalTime localTime) {returntoLocalDateTime(localTime).atZone(ZoneId.systemDefault()).toInstant();
}/*** epochMilli毫秒转Instant
*@paramepochMilli
*@return
*/
public static Instant toInstant(longepochMilli) {
Objects.requireNonNull(epochMilli,"epochMilli");returnInstant.ofEpochMilli(epochMilli);
}/*** temporal转Instant
*@paramtemporal
*@return
*/
public staticInstant toInstant(TemporalAccessor temporal) {returnInstant.from(temporal);
}/*** ZonedDateTime转Instant
* 注意时间对应的时区和默认时区差异
*@paramzonedDateTime
*@return
*/
public staticInstant toInstant(ZonedDateTime zonedDateTime) {
Objects.requireNonNull(zonedDateTime,"zonedDateTime");returnzonedDateTime.toInstant();
}/*** Date转毫秒值
* 从1970-01-01T00:00:00Z开始的毫秒值
*@paramdate
*@return
*/
public static longtoEpochMilli(Date date){
Objects.requireNonNull(date,"date");returndate.getTime();
}/*** LocalDateTime转毫秒值
* 从1970-01-01T00:00:00Z开始的毫秒值
*@paramlocalDateTime
*@return
*/
public static longtoEpochMilli(LocalDateTime localDateTime){returntoInstant(localDateTime).toEpochMilli();
}/*** LocalDate转毫秒值
* 从1970-01-01T00:00:00Z开始的毫秒值
*@paramlocalDate
*@return
*/
public static longtoEpochMilli(LocalDate localDate){returntoInstant(localDate).toEpochMilli();
}/*** Instant转毫秒值
* 从1970-01-01T00:00:00Z开始的毫秒值
*@paraminstant
*@return
*/
public static longtoEpochMilli(Instant instant){
Objects.requireNonNull(instant,"instant");returninstant.toEpochMilli();
}/*** ZonedDateTime转毫秒值,注意时间对应的时区和默认时区差异
* 从1970-01-01T00:00:00Z开始的毫秒值
*@paramzonedDateTime
*@return
*/
public static longtoEpochMilli(ZonedDateTime zonedDateTime) {
Objects.requireNonNull(zonedDateTime,"zonedDateTime");returnzonedDateTime.toInstant().toEpochMilli();
}/*** Date转ZonedDateTime,时区为系统默认时区
*@paramdate
*@return
*/
public staticZonedDateTime toZonedDateTime(Date date) {
Objects.requireNonNull(date,"date");returnInstant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime()
.atZone(ZoneId.systemDefault());
}/*** LocalDateTime转ZonedDateTime,时区为系统默认时区
*@paramlocalDateTime
*@return
*/
public staticZonedDateTime toZonedDateTime(LocalDateTime localDateTime) {
Objects.requireNonNull(localDateTime,"localDateTime");returnlocalDateTime.atZone(ZoneId.systemDefault());
}