本文最后更新于 2024-04-01,欢迎来到我的Blog! https://www.zpeng.site/

Java8新特性新时间日期API

LocalDate、LocalTime、LocalDateTime

1.LocalDateTime

public class test {

    @Test
    public void test() {

        /*
        * LocalDateTime
        * */

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);    // 2021-12-24T14:51:37.161

        //LocalDataTime实例创建
        LocalDateTime ldt = LocalDateTime.of(2021, 12, 23, 13, 23, 32);
        System.out.println(ldt);    // 2021-12-23T13:23:32

        //年份加
        LocalDateTime ldt2 = ldt.plusYears(2);
        System.out.println(ldt2);   // 2023-12-23T13:23:32

        //月份减
        LocalDateTime ldt3 = ldt.minusMonths(2);
        System.out.println(ldt3);   // 2021-10-23T13:23:32

        //获取时间的各个值
        System.out.println(ldt.getYear());          //  2021
        System.out.println(ldt.getMonthValue());    //  12
        System.out.println(ldt.getDayOfMonth());    //  23
        System.out.println(ldt.getHour());          //  13
        System.out.println(ldt.getMinute());        //  23
        System.out.println(ldt.getSecond());        //  32
    }
}

2.Instant:时间戳

(以Unix元年:1970年1月1日 00:00:00 到某个时间之间的毫秒值)

public class test {

    @Test
    public void test() {
        Instant ins = Instant.now();    //  默认获取UTC时区
        System.out.println(ins);

        OffsetDateTime odt = ins.atOffset(ZoneOffset.ofHours(8));   // 带偏移量,这里表示东八区
        System.out.println(odt);

        System.out.println(ins.toEpochMilli()); // 1640329758791
        Instant instant = Instant.ofEpochSecond(60);    // 1970-01-01T00:01:00Z
        System.out.println(instant);
    }
}

3.计算时间间隔

  • Duration:计算两个时间之间的间隔

  • Period:计算两个日期之间的间隔

public class test {

    @Test
    public void test() {
        Instant ins1 = Instant.now();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Instant ins2 = Instant.now();

        Duration duration = Duration.between(ins1, ins2);
        System.out.println(duration.toMillis());    //  1009

        LocalDate ld1 = LocalDate.of(2021,12,12);
        LocalDate ld2 = LocalDate.now();

        Period period = Period.between(ld1, ld2);
        System.out.println(period.getYears());  // 0
        System.out.println(period.getMonths()); // 0
        System.out.println(period.getDays());   // 12
        System.out.println();


    }
}

日期的操纵

4.TemporalAdiuster

时间校正器。有时我们可能需要获取例如:将日期调整到“下个周日”等操作。

public class test {

    @Test
    public void test() {
        LocalDateTime ldt = LocalDateTime.now();
        LocalDateTime ldt2 = ldt.withDayOfMonth(10);    // 将日期设置为10
        System.out.println(ldt);    // 2021-12-24T16:23:16.451
        System.out.println(ldt2);   // 2021-12-10T16:22:40.417

        LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));    // 下一个周日
        System.out.println(ldt3);   // 2021-12-26T16:22:40.417

        //自定义:下一个工作日志
        LocalDateTime ldt5 = ldt.with( l -> {
            LocalDateTime ldt4 = (LocalDateTime) l;
            DayOfWeek dow = ldt4.getDayOfWeek();
            if (dow.equals(DayOfWeek.FRIDAY)) {
                return ldt4.plusDays(3);
            } else if (dow.equals(DayOfWeek.SATURDAY)) {
                return ldt4.plusDays(2);
            } else{
                return ldt4.plusDays(1);
            }
        });
        System.out.println(ldt5);   // 2021-12-27T16:30:55.074
    }
}

5.DateTimeFormatter

格式化时间/日期

    public class test {

    @Test
    public void test() {
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
        LocalDateTime ldt = LocalDateTime.now();

        String strDate = ldt.format(dtf);
        System.out.println(strDate);    // 2021-12-24

        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String strDate2 = dtf2.format(ldt);
        System.out.println(strDate2);   // 2021年12月24日 16:35:55

        LocalDateTime newDate = LocalDateTime.parse(strDate2, dtf2);
        System.out.println(newDate);    // 2021-12-24T16:38:58
    }
}

6.时区的处理

public class test {

    @Test
    public void test() {
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
        System.out.println(ldt);    // 2021-12-24T10:43:37.135

        LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
        ZonedDateTime zdt = ldt2.atZone(ZoneId.of("Europe/Tallinn"));
        System.out.println(zdt);    // 2021-12-24T10:45:08.507+02:00[Europe/Tallinn]
    }
}