Java日時APIシリーズ13---Jdk 8のjava.timeパッケージの新しい日時APIクラス、時間クラス変換、Date転送LocalDateTime、LocalDateTime転送Date

20189 ワード

前のシリーズのブログからJdk 8の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();
    }

}

 
テストクラス:
例:Date変換
Date-LocalDateTimeDate-LocalDateDate-LocalTimeDate-Instant
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