记:JDK8日期时间api

package com.mybatis.springbootmybatis.jdk8.localdate;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class Test {
    public static void main(String[] args) {
        // LocalDate 获取日期、LocalTime获取时间、LocalDateTime获取日期+时间
        //订制日期格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //如果需要使用官方制定的时期格式直接DateTimeFormatter点就可以拿到
        //DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;

        LocalDateTime localDateTime = LocalDateTime.now();
        String format = localDateTime.format(dateTimeFormatter);
        System.out.println(format);

        System.out.println("========================");

        // 获取一天的最大最小时间
        LocalDateTime of = localDateTime.of(LocalDate.now(), LocalTime.MIN);
        of = localDateTime.of(LocalDate.now(), LocalTime.MAX);
        System.out.println(of.format(dateTimeFormatter));

        System.out.println("========================");

        LocalDateTime localDateTime1 = localDateTime.withHour(0).withMinute(0).withSecond(0);
        localDateTime1 = localDateTime.withHour(23).withMinute(59).withSecond(59);
        System.out.println(localDateTime1.format(dateTimeFormatter));

        System.out.println("========================");

        // 当前时间减去2h
        System.out.println(localDateTime1.minusHours(2).format(dateTimeFormatter));

        System.out.println("========================");


        // TemporalAdjuster : 时间校正器。有时我们可能需要获,取例如:将日期调整到“下个周日”等操作。
        // TemporalAdjusters : 该类通过静态方法提供了大量的常用 TemporalAdjuster 的实现。
        // 获取周日时间
        System.out.println(LocalDateTime.now().with(TemporalAdjusters.next(DayOfWeek.SUNDAY)).format(dateTimeFormatter));

        System.out.println("========================");

        // 判断当前时间是哪一天后,进行加减操作
        // DayOfWeek获取周中天
        LocalDateTime localDateTime2 = LocalDateTime.now();
        System.out.println(
                localDateTime2.with((x) -> {
                    LocalDateTime time = (LocalDateTime) x;
                    if (time.getDayOfWeek().equals(DayOfWeek.SUNDAY)) {
                        return time.plusDays(1);
                    }
                    if (time.getDayOfWeek().equals(DayOfWeek.SATURDAY)) {
                        return time.plusDays(2);
                    }
                    return time;
                }).format(dateTimeFormatter)
        );

        System.out.println("========================");

        // Duration : 用于计算两个“时间”间隔
        // Period : 用于计算两个“日期”间隔
        // Instant : 时间戳。 (使用 Unix 元年  1970年1月1日 00:00:00 所经历的毫秒值)
        // ZonedDate、ZonedTime、ZonedDateTime : 带时区的时间或日期
        Instant now = Instant.now();
        LocalTime localTime1 = LocalTime.now();
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Instant now2 = Instant.now();
        System.out.println(Duration.between(now,now2).getSeconds());
        LocalTime localTime2 = LocalTime.now();
        System.out.println(Duration.between(localTime1,localTime2).getSeconds());
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章