Java 8新日時タイプ

1633 ワード

package com.xx;

import org.junit.Test;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author xx
 * DateTime: 2019/11/21 13:42
 * Description: Java8      ,         
 *      LocalDate:  
 *      LocalTime:  
 *      LocalDateTime:  +  
 *      Instant:       
 *
 *      Duration:  2        
 *      Period:  2        
 */
public class TimeTest {

    @Test
    public void test() {
        //         
        LocalDateTime now = LocalDateTime.now();
        System.out.println("    :"+now);
        //     LocalDateTime 
        LocalDateTime of = LocalDateTime.of(2019, 11, 21, 14, 04);
        System.out.println("          :"+of);
        //      
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = now.format(formatter);
        System.out.println("       "+format);
        //    
        System.out.println("   :"+of.plusDays(1));
        //     
        System.out.println("    :"+of.plusMonths(1));
        //    
        System.out.println("   :"+of.minusDays(1));
        //     
        System.out.println("    :"+of.minusMonths(1));
        //          
        System.out.println("         :"+of.getYear());
        //          
        System.out.println("         :"+of.getMonth().getValue() + "  " + of.getMonthValue());
        //   2     
        Long between = Duration.between(of, now).toMillis();
        System.out.println("  2     :"+between);

    }

}