资讯专栏INFORMATION COLUMN

Java 8 常用时间 api

TNFE / 1001人阅读

摘要:常用的有三个类是用来表示时刻的,类似的时间,表示从协调世界时年月日时分秒起至现在的总秒数,也可以获取毫秒。表示一个日期,只有年月日,没有时分秒。还有和方法可以用来比较两个时间。用的是系统默认时区。

Java 8 提供了一套新的时间 api ,比之前的 Calendar 类要简单明了很多。常用的有三个类 InstantLocalDateLocalDateTime , Instant 是用来表示时刻的,类似 Unix 的时间,表示从协调世界时1970年1月1日0时0分0秒起至现在的总秒数,也可以获取毫秒。LocalDate 表示一个日期,只有年月日,没有时分秒。LocalDateTime 就是年月日时分秒了。

Instant
public static void main(String[] args) {
    Instant now = Instant.now();
    System.out.println("Now secoonds:" + now.getEpochSecond());
    System.out.println("Now milli   :" + now.toEpochMilli());
}

输出当前时刻距离 1970年1月1日0时0分0秒 的秒和毫秒

Now secoonds:1541321299
Now milli :1541321299037

LocalDateTime

为了方便输出时间格式,Java8 提供了 DateTimeFormatter 类来替代之前的 SimpleDateFormat

public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime now = LocalDateTime.now();
    System.out.println("Now: " + now.format(formatter));
}

Now: 2018-11-04 16:53:09

LocalDateTime 提供了很多时间计算的方法,比如 加一个小时,减去一周,加上一天等等这样的计算,比之前的 Calendar 要方便许多。

public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime now = LocalDateTime.now();
    System.out.println("Now: " + now.format(formatter));

    LocalDateTime nowPlusDay = now.plusDays(1);
    System.out.println("Now + 1 day: " + nowPlusDay.format(formatter));

    LocalDateTime nowMinusHours = now.minusHours(5);
    System.out.println("Now - 5 hours: " + nowMinusHours.format(formatter));
}

Now: 2018-11-04 17:02:53
Now + 1 day: 2018-11-05 17:02:53
Now - 5 hours: 2018-11-04 12:02:53

LocalDateTime 还有 isAfterisBeforeisEqual 方法可以用来比较两个时间。LocalDate 的用法和 LocalDateTime 是类似的。

Instant 和 LocalDateTime 的互相转换

这俩的互相转换都要涉及到一个时区的问题。LocalDateTime 用的是系统默认时区。我们可以先把 LocalDateTime 转为 ZonedDateTime ,然后再转成 Instant

public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime now = LocalDateTime.now();
    System.out.println("Now: " + now.format(formatter));

    Instant nowInstant = now.atZone(ZoneId.systemDefault()).toInstant();
    System.out.println("Now mini seconds: " + nowInstant.toEpochMilli());
}

Now: 2018-11-04 17:19:16
Now mini seconds: 1541323156101

public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    Instant now = Instant.now();
    System.out.println("Now mini seconds: " + now.toEpochMilli());


    LocalDateTime nowDateTime = LocalDateTime.ofInstant(now, ZoneId.systemDefault());
    System.out.println("Zone id: " + ZoneId.systemDefault().toString());
    System.out.println("Now: " + nowDateTime.format(formatter));
}

Now mini seconds: 1541323844781
Zone id: Asia/Shanghai
Now: 2018-11-04 17:30:44

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71999.html

相关文章

  • java常见类API第二部分

    摘要:类是日期时间格式化子类的抽象类,我们通过这个类可以帮我们完成日期和文本之间的转换也就是可以在对象与对象之间进行来回转换。构造方法由于为抽象类,不能直接使用,所以需要常用的子类。 day01【Object类、常用API】 主要内容 Object类 Date类 DateFormat类 Calendar类 System类 StringBuilder类 包装类 教学目标 -[ ] 能够说出...

    maybe_009 评论0 收藏0
  • 20 个案例教你在 Java 8 中如何处理日期和时间?

    摘要:前言前面一篇文章写了如何安全的使用里面介绍了如何处理日期时间,以及如何保证线程安全,及其介绍了在中的处理时间日期默认就线程安全的类。引入了全新的日期时间格式工具,线程安全而且使用方便。 前言 前面一篇文章写了《SimpleDateFormat 如何安全的使用?》, 里面介绍了 SimpleDateFormat 如何处理日期/时间,以及如何保证线程安全,及其介绍了在 Java 8 中的处...

    Rango 评论0 收藏0
  • Java面试 32个核心必考点完全解析

    摘要:如问到是否使用某框架,实际是是问该框架的使用场景,有什么特点,和同类可框架对比一系列的问题。这两个方向的区分点在于工作方向的侧重点不同。 [TOC] 这是一份来自哔哩哔哩的Java面试Java面试 32个核心必考点完全解析(完) 课程预习 1.1 课程内容分为三个模块 基础模块: 技术岗位与面试 计算机基础 JVM原理 多线程 设计模式 数据结构与算法 应用模块: 常用工具集 ...

    JiaXinYi 评论0 收藏0
  • Javag工程师成神之路(2019正式版)

    摘要:结构型模式适配器模式桥接模式装饰模式组合模式外观模式享元模式代理模式。行为型模式模版方法模式命令模式迭代器模式观察者模式中介者模式备忘录模式解释器模式模式状态模式策略模式职责链模式责任链模式访问者模式。 主要版本 更新时间 备注 v1.0 2015-08-01 首次发布 v1.1 2018-03-12 增加新技术知识、完善知识体系 v2.0 2019-02-19 结构...

    Olivia 评论0 收藏0
  • Java™ 教程(Date-Time)

    Date-Time Java SE 8发行版中引入的Date-Time包java.time提供了全面的日期和时间模型,是在JSR 310:Date and Time API下开发的,尽管java.time基于国际标准化组织(ISO)日历系统,但也支持常用的全球日历。 此课程介绍了使用基于ISO的类来表示日期和时间以及操作日期和时间值的基本原理。 概述 时间似乎是一个简单的主题,即便是便宜的手表也能...

    AlphaGooo 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<