zoukankan      html  css  js  c++  java
  • JDK 8 中时间API

    一、分类

    1、时间对象

    • TemporalAccessor: 顶级接口
    • Temporal: 子接口,继承TemporalAccessor接口
    • Era: 子接口,继承TemporalAccessor接口
    • Instant: 时间戳,有时区(UTC时区),实现Temporal、TemporalAdjuster接口
    • LocalDate: 日期,无指定时区,实现Temporal、TemporalAdjuster接口
    • LocalTime: 时间,指定时区,实现Temporal、TemporalAdjuster接口
    • LocalDateTime: 日期时间,指定时区,实现Temporal、TemporalAdjuster接口
    • OffsetTime: 偏移后时间,指定时区,实现Temporal、TemporalAdjuster接口
    • OffsetDateTime: 偏移后日期时间,无指定时区,实现Temporal、TemporalAdjuster接口
    • ZonedDateTime: 时区日期时间,无指定时区,实现Temporal、TemporalAdjuster接口

    2、时区、偏移量

    • ZoneId: 时区
    • ZoneOffset: 时区偏移量

    3、工具类

    • TemporalAdjuster: 功能型接口
    • TemporalAdjusters: 工具类,每个方法中生成一个匿名类,实现了TemporalAdjuster接口
    // 下一个周几
    public static TemporalAdjuster next(DayOfWeek dayOfWeek) {
        int dowValue = dayOfWeek.getValue();
        return (temporal) -> {
            int calDow = temporal.get(DAY_OF_WEEK);
            int daysDiff = calDow - dowValue;
            return temporal.plus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
        };
    }
    // 下一个周一时间
    LocalDate ld = LocalDate.now();
    System.out.println("LocalDate: " + ld);
    ld = ld.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
    System.out.println("LocalDate next TUESDAY: " + ld);
    
    // LocalDate: 2019-12-30
    // LocalDate next MONDAY: 2020-01-06
    // with方法内部调用了匿名类的adjustInto方法
    public LocalDate with(TemporalAdjuster adjuster) {
        // optimizations
        if (adjuster instanceof LocalDate) {
            return (LocalDate) adjuster;
        }
        return (LocalDate) adjuster.adjustInto(this);
    }

    4、日期格式化

    • DateTimeFormatter
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
  • 相关阅读:
    GPIO推挽输出和开漏输出详解
    Linux驱动中completion接口浅析(wait_for_complete例子,很好)【转】
    当JAVA集合移除自身集合元素时发生的诸多问题
    Machine Learning #Lab1# Linear Regression
    Nth to Last Node in List
    Codeforces Round #272 (Div. 2)AK报告
    iOS 使用Block实现函数回调
    ios上禁止输入表情
    POJ 1287:Networking(最小生成树Kruskal)
    CSS实现强制换行-------Day 78
  • 原文地址:https://www.cnblogs.com/pascall/p/12119131.html
Copyright © 2011-2022 走看看