Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date

  从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转Date。下面是时间类互相转换大全,包含Instant、LocalDate、LocalDateTime、LocalTime和Date的相互转换,下面是一个工具类,仅供参考:

package com.xkzhangsan.time.converter;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.Objects;

/**
 * 
* @ClassName: DateTimeConverterUtil 
* @Description: DateTime Converter
* @author xkzhangsan
* @date 2019年12月1日
*
 */
public class DateTimeConverterUtil {

    public static Date toDate(LocalDateTime localDateTime) {
        Objects.requireNonNull(localDateTime, "localDateTime");
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date toDate(LocalDate localDate) {
        Objects.requireNonNull(localDate, "localDate");
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }
    
    /**
     * 以当天的日期+LocalTime组成新的LocalDateTime转换为Date
     * @param localTime
     * @return
     */
    public static Date toDate(LocalTime localTime) {
        Objects.requireNonNull(localTime, "localTime");
        return Date.from(LocalDate.now().atTime(localTime).atZone(ZoneId.systemDefault()).toInstant());
    }    

    public static Date toDate(Instant instant) {
        return Date.from(instant);
    }
    
    public static Date toDate(Long epochMilli){
        Objects.requireNonNull(epochMilli, "epochMilli");
        return new Date(epochMilli);
    }

    public static LocalDateTime toLocalDateTime(Date date) {
        Objects.requireNonNull(date, "date");
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static LocalDateTime toLocalDateTime(LocalDate localDate) {
        Objects.requireNonNull(localDate, "localDate");
        return localDate.atStartOfDay();
    }
    
    /**
     * 以当天的日期+LocalTime组成新的LocalDateTime
     * @param localTime
     * @return
     */
    public static LocalDateTime toLocalDateTime(LocalTime localTime) {
        Objects.requireNonNull(localTime, "localTime");
        return LocalDate.now().atTime(localTime);
    }

    public static LocalDateTime toLocalDateTime(Instant instant) {
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }
    
    public static LocalDateTime toLocalDateTime(Long epochMilli) {
        Objects.requireNonNull(epochMilli, "epochMilli");
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
    }
    
    public static LocalDateTime toLocalDateTime(TemporalAccessor temporal) {
        return LocalDateTime.from(temporal);
    }    

    public static LocalDate toLocalDate(Date date) {
        return toLocalDateTime(date).toLocalDate();
    }

    public static LocalDate toLocalDate(LocalDateTime localDateTime) {
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.toLocalDate();
    }

    public static LocalDate toLocalDate(Instant instant) {
        return toLocalDateTime(instant).toLocalDate();
    }
    
    public static LocalDate toLocalDate(TemporalAccessor temporal) {
        return LocalDate.from(temporal);
    }    

    public static LocalTime toLocalTime(Date date) {
        return toLocalDateTime(date).toLocalTime();
    }

    public static LocalTime toLocalTime(LocalDateTime localDateTime) {
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.toLocalTime();
    }

    public static LocalTime toLocalTime(Instant instant) {
        return toLocalDateTime(instant).toLocalTime();
    }
    
    public static LocalTime toLocalTime(TemporalAccessor temporal) {
        return LocalTime.from(temporal);
    }    

    public static Instant toInstant(Date date) {
        Objects.requireNonNull(date, "date");
        return date.toInstant();
    }

    public static Instant toInstant(LocalDateTime localDateTime) {
        Objects.requireNonNull(localDateTime, "localDateTime");
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant();
    }

    public static Instant toInstant(LocalDate localDate) {
        return toLocalDateTime(localDate).atZone(ZoneId.systemDefault()).toInstant();
    }
    
    /**
     * 以当天的日期+LocalTime组成新的LocalDateTime转换为Instant
     * @param localTime
     * @return
     */
    public static Instant toInstant(LocalTime localTime) {
        return toLocalDateTime(localTime).atZone(ZoneId.systemDefault()).toInstant();
    }
    
    public static Instant toInstant(Long epochMilli) {
        Objects.requireNonNull(epochMilli, "epochMilli");
        return Instant.ofEpochMilli(epochMilli);
    }
    
    public static Instant toInstant(TemporalAccessor temporal) {
        return Instant.from(temporal);
    }
    
    /**
     * 转换为毫秒值
     * 从1970-01-01T00:00:00Z开始的毫秒值
     * @param date
     * @return
     */
    public static Long toEpochMilli(Date date){
        Objects.requireNonNull(date, "date");
        return date.getTime();
    }
    
    /**
     * 转换为毫秒值
     * 从1970-01-01T00:00:00Z开始的毫秒值
     * @param localDateTime
     * @return
     */
    public static Long toEpochMilli(LocalDateTime localDateTime){
        return toInstant(localDateTime).toEpochMilli();
    }
    
    /**
     * 转换为毫秒值
     * 从1970-01-01T00:00:00Z开始的毫秒值
     * @param localDate
     * @return
     */
    public static Long toEpochMilli(LocalDate localDate){
        return toInstant(localDate).toEpochMilli();
    }
    
    /**
     * 转换为毫秒值
     * 从1970-01-01T00:00:00Z开始的毫秒值
     * @param instant
     * @return
     */
    public static Long toEpochMilli(Instant instant){
        Objects.requireNonNull(instant, "instant");
        return instant.toEpochMilli();
    }

}

测试类:

package com.xkzhangsan.time.test;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

import org.junit.Test;

import com.xkzhangsan.time.converter.DateTimeConverterUtil;

public class ConverterTest {
    
    @Test
    public void dateConverterTest(){
        System.out.println("===================dateConverterTest=====================");
        Date date = new Date();
        System.out.println(DateTimeConverterUtil.toLocalDateTime(date));
        System.out.println(DateTimeConverterUtil.toLocalDate(date));
        System.out.println(DateTimeConverterUtil.toLocalTime(date));
        System.out.println(DateTimeConverterUtil.toInstant(date));
    }
    
    @Test
    public void localDateTimeConverterTest(){
        System.out.println("===================localDateTimeConverterTest=====================");
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println(DateTimeConverterUtil.toDate(ldt));
        System.out.println(DateTimeConverterUtil.toLocalDate(ldt));
        System.out.println(DateTimeConverterUtil.toLocalTime(ldt));
        System.out.println(DateTimeConverterUtil.toInstant(ldt));
    }
    
    @Test
    public void localDateConverterTest(){
        System.out.println("===================localDateConverterTest=====================");
        LocalDate ld = LocalDate.now();
        System.out.println(ld);
        System.out.println(DateTimeConverterUtil.toDate(ld));
        System.out.println(DateTimeConverterUtil.toLocalDateTime(ld));
        System.out.println(DateTimeConverterUtil.toInstant(ld));
    }
    
    @Test
    public void localTimeConverterTest(){
        System.out.println("===================localTimeConverterTest=====================");
        LocalTime lt = LocalTime.now();
        System.out.println(lt);
        System.out.println(DateTimeConverterUtil.toDate(lt));
        System.out.println(DateTimeConverterUtil.toLocalDateTime(lt));
        System.out.println(DateTimeConverterUtil.toLocalTime(lt));
        System.out.println(DateTimeConverterUtil.toInstant(lt));
    }    

    
    @Test
    public void instantConverterTest(){
        System.out.println("===================instantConverterTest=====================");
        Instant instant = Instant.now();
        System.out.println(instant);
        System.out.println(DateTimeConverterUtil.toDate(instant));
        System.out.println(DateTimeConverterUtil.toLocalDateTime(instant));
        System.out.println(DateTimeConverterUtil.toLocalDate(instant));
    }
}

输出结果:

===================dateConverterTest=====================
2020-01-05T22:41:56.606
2020-01-05
22:41:56.606
2020-01-05T14:41:56.606Z
===================localTimeConverterTest=====================
22:41:56.706
Sun Jan 05 22:41:56 CST 2020
2020-01-05T22:41:56.706
22:41:56.706
2020-01-05T14:41:56.706Z
===================localDateConverterTest=====================
2020-01-05
Sun Jan 05 00:00:00 CST 2020
2020-01-05T00:00
2020-01-04T16:00:00Z
===================localDateTimeConverterTest=====================
2020-01-05T22:41:56.718
Sun Jan 05 22:41:56 CST 2020
2020-01-05
22:41:56.718
2020-01-05T14:41:56.718Z
===================instantConverterTest=====================
2020-01-05T14:41:56.719Z
Sun Jan 05 22:41:56 CST 2020
2020-01-05T22:41:56.719
2020-01-05

git地址:https://github.com/xkzhangsan/xk-time

猜你喜欢

转载自www.cnblogs.com/xkzhangsanx/p/12154042.html