Java基础(LocalDate, LocalTime, LocalDateTime)

package com.klvchen.java;

import org.junit.Test;

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

public class JDK8DateTimeTest {

    /*
    LocalDate, LocalTime, LocalDateTime 的使用
     */

    @Test
    public void test1(){
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);
        System.out.println("=================================================");

        //of():设置指定的年,月,日,时,分,秒,没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
        System.out.println(localDateTime1);
        System.out.println("=================================================");

        //getXxx()
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMinute());
        System.out.println("=================================================");

        //体现不可变性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System.out.println(localDate);
        System.out.println(localDate1);
        System.out.println("=================================================");

        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);
        System.out.println("=================================================");

    }
}

Java基础(LocalDate, LocalTime, LocalDateTime)

Java基础(LocalDate, LocalTime, LocalDateTime)

上一篇:【Java代码之美】 -- 通过Value获取Map中的键值Key的四种方法


下一篇:Java 程序运行机制