Java的Duration类如何使用


本文小编为大家详细介绍“Java的Duration类如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Java的Duration类如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。说明Duration类通过秒和纳秒相结合来描述一个时间量,最高精度是纳秒。时间量可以为正也可以为负,比如1天(86400秒0纳秒)、-1天(-86400秒0纳秒)、1年(31556952秒0纳秒)、1毫秒(0秒1000000纳秒)等。Period类通过年、月、日相结合来描述一个时间量,最高精度是天。时间量可以为正也可以为负,例如2年(2年0个月0天)、3个月(0年3个月0天)、4天(0年0月4天)等。这两个类是不可变的、线程安全的、最终类。都是JDK8新增的。Period用法见:详解Java中Period类的使用方法基于天、时、分、秒、纳秒创建。ofDays(), ofHours(), ofMillis(), ofMinutes(), ofNanos(), ofSeconds()。例如:通过LocalDateTime或者LocalTime 类,然后使用between获取创建Duration。用法示例格式说明采用ISO-8601时间格式。格式为:PnYnMnDTnHnMnS (n为个数)例如:P1Y2M10DT2H30M15.03SP:开始标记1Y:一年2M:两个月10D:十天T:日期和时间的分割标记2H:两个小时30M:三十分钟15S:15.02秒1.”P”, “D”, “H”, “M” 和 “S”可以是大写或者小写(建议大写)2.可以用“-”表示负数示例大全”PT20.345S” — parses as “20.345 seconds”
“PT15M” — parses as “15 minutes” (where a minute is 60 seconds)
“PT10H” — parses as “10 hours” (where an hour is 3600 seconds)
“P2D” — parses as “2 days” (where a day is 24 hours o免费云主机域名r 86400 seconds)
“P2DT3H4M” — parses as “2 days, 3 hours and 4 minutes”
“P-6H3M” — parses as “-6 hours and +3 minutes”
“-P6H3M” — parses as “-6 hours and -3 minutes”
“-P-6H+3M” — parses as “+6 hours and -3 minutes”源码:
*Thiswillparseatextualrepresentationofaduration,includingthe
*stringproducedby{@codetoString()}.Theformatsacceptedarebased
*ontheISO-8601durationformat{@codePnDTnHnMn.nS}withdays
*consideredtobeexactly24hours.
*
*Thestringstartswithanoptionalsign,denotedbytheASCIInegative
*orpositivesymbol.Ifnegative,thewholeperiodisnegated.
*TheASCIIletter”P”isnextinupperorlowercase.
*Therearethenfoursections,eachconsistingofanumberandasuffix.
*ThesectionshavesuffixesinASCIIof”D”,”H”,”M”and”S”for
*days,hours,minutesandseconds,acceptedinupperorlowercase.
*Thesuffixesmustoccurinorder.TheASCIIletter”T”mustoccurbefore
*thefirstoccurrence,ifany,ofanhour,minuteorsecondsection.
*Atleastoneofthefoursectionsmustbepresent,andif”T”ispresent
*theremustbeatleastonesectionafterthe”T”.
*ThenumberpartofeachsectionmustconsistofoneormoreASCIIdigits.
*ThenumbermaybeprefixedbytheASCIInegativeorpositivesymbol.
*Thenumberofdays,hoursandminutesmustparsetoan{@codelong}.
*Thenumberofsecondsmustparsetoan{@codelong}withoptionalfraction.
*Thedecimalpointmaybeeitheradotoracomma.
*Thefractionalpartmayhavefromzeroto9digits.
*
*Theleadingplus/minussign,andnegativevaluesforotherunitsare
*notpartoftheISO-8601standard.
*
*Examples:
*比较两个时间的差plusX()、minusX()X表示days, hours, millis, minutes, nanos 或 secondsplus()/minus()方法带TemporalUnit 类型参数进行加减:可以用toX来转换为其他单位,支持:toDays, toHours, toMinutes, toMillis, toNanos可以用getX来获得指定位置的值,因为Duration是由秒和纳秒组成,所以只能获得秒和纳秒:读到这里,这篇“Java的Duration类如何使用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注百云主机行业资讯频道。

相关推荐: Vue3中的插槽怎么使用

本篇内容主要讲解“Vue3中的插槽怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Vue3中的插槽怎么使用”吧! 简单来说就是子组件中的提供给父组件使用的一个坑位,用 表示,父组件可以在这个坑位中填充任何模板…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 05/07 11:20
下一篇 05/07 11:21

相关推荐