Java里time包的功能,java8新特性教程之time包使用总结

前言

java8新特性java.time.*包学习。 自从java发布模式变更就发现自己有些跟不上他们的速度,java8还有不少没有用透而9、10、11相继出来,长江后浪推前浪一浪胜过一浪。之前date的使用还不敢自信说多透彻,后续都是泪...(欢迎酱油...)

以jdk1.8.0_111为例

新的设计思路

引入final定义支持时间点不可变和线程安全,长久来的date的设计一直遭人诟病着;

设计localdate、localdatetime、localtime、instant、clock、duration等类,format\zoo\temporal等包规范时间的定义划分;

时间统一使用 iso-8601 日历系统,也就是yyyy-mm-dd't'hh🇲🇲ss:ssszz格式,输出2012-04-13t10:53:43:119+08:00样子,要是用过jota-time包估计你什么都懂了;

规范并提供更加好用的时间操作方法,plus\minus\with\to\get\of\now等方法命名规则;

jdk1.8包目录简介:

2570789f6ebbb1dafa85424e1b58d252.png

time:父级基础包,常用的时间相关类都在这里,如localdate\localdatetime\instant等

chrono:日历系统包,日历相关的接口(类似calendar)也包括提供对其他日历系统的api

format:格式化和解析包,主要类是datetimeformatter

temporal:扩展功能包,提供细粒度的时间控制field、unit,如weeks、months、month-of-year等

zone:时区包,时区规则、本地时区等

83b3995833847361fb932b27d6e4f222.png

26cf674c0be612510cf011ceec74b3e4.png

69eae4c9f2a8b2591d022618f7f2ef6f.png

e53a0961132aecfd8e6c9a76f7ae613b.png

围绕常用点和大多数人经常用到做些用例

计算、格式化、字符串转换这三快可以说是很基础的功能,大多数人主要围绕这几块进行开发和封装,基础掌握后面的高级用法慢慢就可以积累。一般使用都是围绕time基础包中localdate\localdatetime\localtime类展开,先介绍一下这几个类及相关的类。

localdate: 一个iso-8601日历系统下的data对象,如2007-12-03 localdatetime: 一个iso-8601日历系统下的data-time对象,如2007-12-03t10:15:30 localtime: 一个iso-8601日历系统下的time对象,如10:15:30 instant: 一个瞬时的时间点值对象,从1970-01-01t00:00:00z点毫秒计算的 clock: 基于instant生成的时钟对象,遵守utc时区规则可以去生成date和time duration: 一个time范围值对象,单位也可以是分钟或小时 period: 一个date范围值对象,单位可以是年、月、日,和duration正好是两个粒度对象 offsetdatetime: 从utc/greenwich格式时间偏移成iso-8601的date-time对象,如2007-12-03t10:15:30+01:00 offsettime: 和offsetdatetime类似,粒度是到time的utc时间格式对象,如10:15:30+01:00 zoneddatetime: 带时区的iso-8601日历系统的date-time对象,如2007-12-03t10:15:30+01:00 europe/paris zonedoffset: 一个带时区的从greenwich/utc的偏移量范围中对象,如+02:00

网上找的新旧类的比较图挺好的,贴在这里:

1363a1951201d3504c637dfc991f0f1f.png

时间加减计算

localdatetime datetime = localdatetime.now(clock.system(zoneid.systemdefault()));

localdatetime datetime2 =datetime.minusdays(2);

printtest(datetime);

printtest(datetime2);

printtest(datetime2.plushours(3));

printtest(datetime2.minusweeks(1));

printtest(datetime2.plus(1,chronounit.months));

printtest(datetime2.compareto(datetime));

printtest(datetime2.withyear(2));

printtest(datetime2.isbefore(datetime));

duration duration = duration.ofdays(5);

printtest(duration);

printtest(duration.plushours(2).tominutes());

结果:

1 : 2019-01-29 11:01:49

2 : 2019-01-27 11:01:49

3 : 2019-01-27 14:01:49

4 : 2019-01-20 11:01:49

5 : 2019-02-27 11:01:49

6 : -2

7 : 0002-01-27 11:01:49

8 : true

9 : pt120h

10 : 7320

时间输出格式化

localdatetime datetime = localdatetime.now(clock.systemdefaultzone());

printtest(datetime);

printtest(datetime.format(datetimeformatter.basic_iso_date));

printtest(datetime.format(datetimeformatter.iso_local_date_time));

printtest(datetime.format(datetimeformatter.iso_week_date));

printtest(datetime.format(datetimeformatter.ofpattern("yyyy-mm-dd hh🇲🇲ss.sss")));

datetimeformatterbuilder builder = new datetimeformatterbuilder();

builder.appendpattern("yyyy-mm-dd");

builder.parsestrict().toformatter();

printtest(datetime.format(builder.parsestrict().toformatter()));

结果:

1 : 2019-01-29 11:14:07

2 : 20190129

3 : 2019-01-29t11:14:07.232

4 : 2019-w05-2

5 : 2019-01-29 11:14:07.232

6 : 2019-01-29

时间对象和string相互转换

localdatetime datetime = localdatetime.now(clock.system(zoneid.systemdefault()));

printtest(datetime.format(datetimeformatter.iso_local_date));

printtest(datetime.format(datetimeformatter.iso_local_date_time));

printtest(datetime.format(datetimeformatter.iso_local_time));

printtest(datetime.format(datetimeformatter.basic_iso_date));

printtest(datetime.format(datetimeformatter.ofpattern("yyyy-mm-dd hh🇲🇲ss")));

printtest(datetime.format(datetimeformatter.oflocalizeddatetime(formatstyle.short)));

printtest(datetime.format(datetimeformatter.oflocalizeddatetime(formatstyle.medium)));

printtest(datetime.format(datetimeformatter.oflocalizeddatetime(formatstyle.valueof("medium"))));

printtest(localdatetime.parse("2019-12-03t10:15:30").tostring());

printtest(localdate.parse("2019-12-03",datetimeformatter.iso_local_date));

printtest(localtime.parse("10:15:30",datetimeformatter.iso_local_time));

结果:

1 : 2019-01-29

2 : 2019-01-29t10:35:38.508

3 : 10:35:38.508

4 : 20190129

5 : 2019-01-29 10:35:38

6 : 19-1-29 上午10:35

7 : 2019-1-29 10:35:38

8 : 2019-1-29 10:35:38

9 : 2019-12-03t10:15:30

10 : 2019-12-03

11 : 10:15:30

新api和就date转换策略

localdatetime localdatetime = localdatetime.now();

localdatetime.minushours(2);

printtest(localdatetime);

date localdatetime2 = date.from(localdatetime.atzone(zoneid.systemdefault()).toinstant());

printtest(localdatetime2.tostring());

localdate localdate = localdate.now();

printtest(localdate);

date localdate2 = date.from(localdate.atstartofday().atzone(zoneid.systemdefault()).toinstant());

printtest(localdate2);

date date = new date();

printtest(date);

localdatetime date2 = localdatetime.ofinstant(date.toinstant(),zoneid.systemdefault());

printtest(date2);

localtime localtime = localdatetime.ofinstant(new date().toinstant(),zoneid.systemdefault()).tolocaltime();

printtest(localtime);

结果:

1 : 2019-01-29 13:06:58

2 : tue jan 29 13:06:58 cst 2019

3 : 2019-01-29

4 : tue jan 29 00:00:00 cst 2019

5 : tue jan 29 13:06:58 cst 2019

6 : 2019-01-29 13:06:58

7 : 13:06:58.343

想学好一件东西,强烈建议你买本书看看

不要听信那些“网上找找文档看看就能掌握”的鬼话,过来人都明白知识体系的分量有多重要。自己找的资料是非常零碎的,同时需要你很强大的知识构建能力去把这些都串起来。很不幸,这样的能力绝大多数人都不具备。

每一本技术书的完成都不是一朝一夕随随便便就写出来的,作者用心良苦的在思考如何帮助你去理解。写书和技术能力有时不一定成正比,但逻辑思维能力应该不弱。反正写技术书的人在我心里都是神一般的存在,虽然垃圾书比较多但不影响我对他们的仰慕。

推荐

常用java处理时间包:

java8-api文档:

总结

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

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值