前言
java 8 提供了一套新的时间 api ,比之前的 calendar 类要简单明了很多。常用的有三个类 instant、localdate 、localdatetime , instant 是用来表示时刻的,类似 unix 的时间,表示从协调世界时1970年1月1日0时0分0秒起至现在的总秒数,也可以获取毫秒。localdate 表示一个日期,只有年月日,没有时分秒。localdatetime 就是年月日时分秒了。
下面话不多说了,来一起看看详细的介绍吧
instant
|
1
2
3
4
5
|
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。
|
1
2
3
4
5
|
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 要方便许多。
|
1
2
3
4
5
6
7
8
9
10
11
|
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。
|
1
2
3
4
5
6
7
8
|
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
|
1
2
3
4
5
6
7
8
9
10
|
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
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。
原文链接:https://juejin.im/post/5bdebd3ee51d4576452d224f
相关文章
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 51
-
2025-05-27 53
-
2025-05-29 59
-
2025-05-25 53
-
2025-05-24 56

