Java8的常用时间api实用指南

所属分类: 软件编程 / java 阅读数: 51
收藏 0 赞 0 分享

前言

Java 8 提供了一套新的时间 api ,比之前的 Calendar 类要简单明了很多。常用的有三个类 Instant、LocalDate 、LocalDateTime , 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 还有 isAfter 、 isBefore 和 isEqual 方法可以用来比较两个时间。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

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

spring boot 静态资源处理方法

本篇文章主要介绍了spring boot 静态资源处理方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解spring boot 使用application.properties 进行外部配置

这篇文章主要介绍了详解spring boot 使用application.properties 进行外部配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java实现分页的前台页面和后台代码

这篇文章主要为大家详细介绍了Java实现分页的前台页面和后台代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解spring面向切面aop拦截器

spring中有很多概念和名词,比如过滤器、拦截器、aop等。这篇文章主要介绍了详解spring面向切面aop拦截器,有兴趣的可以了解一下。
收藏 0 赞 0 分享

Java easyui树形表格TreeGrid的实现代码

这篇文章主要为大家详细介绍了Java easyui树形表格TreeGrid的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java的NIO与IO的详解及对比

这篇文章主要介绍了Java的NIO与IO的详解及对比的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中网络IO的实现方式(BIO、NIO、AIO)介绍

这篇文章主要介绍了Java中网络IO的实现方式(BIO、NIO、AIO)介绍的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 敏感信息加密处理

本文主要介绍了Java 敏感信息加密处理的相关知识:1)敏感信息加密处理我们要实现什么;2)敏感信息加密处理我做了些什么;3)敏感信息加密实现方法。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详解使用Spring3 实现用户登录以及权限认证

这篇文章主要介绍了详解使用Spring3 实现用户登录以及权限认证,这里整理了详细的代码,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

Java的内存机制详解

本文主要介绍了Java的内存机制的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享
查看更多