好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java8的常用时间api实用指南

前言

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

总结

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

原文链接:https://juejin.im/post/5bdebd3ee51d4576452d224f

查看更多关于Java8的常用时间api实用指南的详细内容...

  阅读:9次