Java8新特性 -- 时间类

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;

/**
 * @description: 验证JDK8的日期和时间类
 * @author: xxx
 * @date: 2021/12/02 22:08
 */
public class TestLocalDateTime {
    /**
    * 验证:LocalDate 日期 yyyy-MM-dd
    **/
    @Test
    public void testLocalDate(){
        LocalDate.of(1985, 8, 6);
        LocalDate.of(1985, Month.AUGUST,8);
        LocalDate.ofYearDay(1985,218);
        //从1970年之后的N天的日期
        LocalDate.ofEpochDay(1);

        LocalDate now = LocalDate.now();
        now.getYear();
        now.getMonth().getValue();
        now.getDayOfMonth();
        now.getDayOfWeek();
        now.getDayOfYear();
    }

    /**
    * 验证:LocalTime 时间 HH:mm:ss(.ns)
    **/
    @Test
    public void testLocalTime(){
        LocalTime.of(11, 15, 12, 234_444_933);
        LocalTime.ofSecondOfDay(100L);
        LocalTime.of(1, 1, 1);

        LocalTime now = LocalTime.now();
        now.getHour();
        now.getMinute();
        now.getSecond();
        now.getNano();
    }

    /**
    * 验证:LocalDateTime 日期时间 yyyy-MM-ddTHH:mm:ss(.ns)
    **/
    @Test
    public void testLocalDateTime(){
        LocalDateTime.of(1908, 12, 12, 12, 12, 12, 12);
        LocalDateTime now = LocalDateTime.now();
        now.getYear();
        now.getMonthValue();
        now.getDayOfMonth();
        now.getDayOfYear();
        now.getDayOfWeek();
        now.getHour();
        now.getMinute();
        now.getSecond();
        now.getNano();
    }

    /**
    * 验证:日期时间的操作
     * 日期时间对象都是final修饰,不可变
    **/
    @Test
    public void testLocalDateTimeOption(){
        //修改日期时间 不对原始对象做修改 withAttribute
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime setYear = now.withYear(1993);
        System.out.println(now);
        System.out.println(setYear);

        //在当前对象基础上加上or减去指定时间  plus | minus
        now.plusDays(2);
        now.plusMonths(2);
        now.minusYears(1);

        //日期比较 isBefore() isAfter() equal()
        LocalDate of = LocalDate.of(1998, 12, 12);
        System.out.println(LocalDate.now().isAfter(of));

        //日期格式化,默认格式 yyyy-MM-ddTHH:mm:ss
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(now.format(formatter));

        System.out.println(LocalDateTime.parse("1993-12-12 12:12:12", formatter));
        System.out.println(LocalDateTime.parse("1993-12-12T12:12:12"));

        /**
         * 日期时间差
         *      Duration: LocalTime之间的距离对象
         *      Period: LocalDate之间的距离对象
         */
        Duration duration = Duration.between(LocalTime.of(18, 12, 12), LocalTime.now());
        System.out.println(duration.toDays());
        System.out.println(duration.toHours());
        System.out.println(duration.toMinutes());

        //10天 = 240H
        System.out.println(Duration.ofDays(10));
        //1H
        System.out.println(Duration.ofHours(1));
        //6M
        System.out.println(Duration.ofMinutes(6));
        //10S
        System.out.println(Duration.ofSeconds(10));

        Period period = Period.between(LocalDate.now(), LocalDate.now().plusDays(1000));
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }


    /**
    * 验证:Instant类 -- 时间戳
    **/
    @Test
    public void testInstant(){
        Instant now = Instant.now();
        System.out.println(now);

        //epoch 到现在的秒
        System.out.println(now.getEpochSecond());
        //纳秒数指的是该秒内的纳秒时间
        System.out.println(now.getNano());

        //epoch 到现在的 毫秒
        System.out.println(now.toEpochMilli());
        //等同于toEpochMilli
        System.out.println(System.currentTimeMillis());

        //epoch 之后的5秒
        System.out.println(Instant.ofEpochSecond(5));
    }

    /**
    * 验证:时间校正器
    **/
    @Test
    public void testTimeTemporalAdjuster(){
        LocalDateTime now = LocalDateTime.now();
        //下个月的第一天
        TemporalAdjuster temporalAdjuster = new TemporalAdjuster() {
            @Override
            public Temporal adjustInto(Temporal temporal) {
                //这里temporal = now()
                LocalDateTime now = (LocalDateTime)temporal;
                System.out.println(now);
                LocalDateTime nextMonth = now.plusMonths(1).withDayOfMonth(1);
                return nextMonth;
            }
        };
        System.out.println(now.with(temporalAdjuster));
    }

    /**
    * 验证:时区
     *      LocalDate/DateTime/Time 不带时区
     *      ZoneDate/DateTime/Time  带时区
    **/
    @Test
    public void testTimeZone(){
        //获取所有的时区ID,格式为  区域/城市
        //ZoneId.getAvailableZoneIds().forEach(System.out::println);

        //根据当前系统的时区now(Clock.systemDefaultZone()),返回时间
        System.out.println(LocalDateTime.now());

        //获取带时区的类,世界标准时间
        System.out.println(ZonedDateTime.now(Clock.systemUTC()));
        System.out.println(ZonedDateTime.now());

        //指定时区
        System.out.println(ZonedDateTime.now(ZoneId.of("America/Vancouver")));
    }

}

上一篇:Java日期和时间类总结


下一篇:Java8新特性时间日期库