详解Java中Period类的使用方法
作者:IT利刃出鞘 时间:2023-11-28 21:04:44
简介
本文用示例介绍java的Period的用法。
Duration和Period
说明
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新增的。
Duration用法
见:详解Java中Duration类的使用方法
创建方法
通过时间单位创建
如果仅一个值表示,如使用ofDays()方法,那么其他值为0。
若仅用ofWeeks,则其天数为week数乘以7.
Period fromUnits = Period.of(3, 10, 10);
Period fromDays = Period.ofDays(50);
Period fromMonths = Period.ofMonths(5);
Period fromYears = Period.ofYears(10);
Period fromWeeks = Period.ofWeeks(40); //280天
通过LocalDate创建
LocalDate startDate = LocalDate.of(2015, 2, 20);
LocalDate endDate = LocalDate.of(2017, 1, 15);
// startDate减endDate
Period period = Period.between(startDate, endDate);
解析方法
格式1:“PnYnMnWnD”
P:开始符,表示period(即:表示年月日);
Y:year;
M:month;
W:week;
D:day
P, Y, M, W, D都可以用大写或者小写。
Period period = Period.parse("P2Y"); //2年
Period period = Period.parse("P2Y3M5D"); //2年3月5天
Period period = Period.parse("P1Y2M3W4D"); // 1年2月3周4天。即:1年2月25天
源码
public final class Period
implements ChronoPeriod, Serializable {
//-----------------------------------------------------------------------
/**
* Obtains a {@code Period} from a text string such as {@code PnYnMnD}.
* <p>
* This will parse the string produced by {@code toString()} which is
* based on the ISO-8601 period formats {@code PnYnMnD} and {@code PnW}.
* <p>
* The string starts with an optional sign, denoted by the ASCII negative
* or positive symbol. If negative, the whole period is negated.
* The ASCII letter "P" is next in upper or lower case.
* There are then four sections, each consisting of a number and a suffix.
* At least one of the four sections must be present.
* The sections have suffixes in ASCII of "Y", "M", "W" and "D" for
* years, months, weeks and days, accepted in upper or lower case.
* The suffixes must occur in order.
* The number part of each section must consist of ASCII digits.
* The number may be prefixed by the ASCII negative or positive symbol.
* The number must parse to an {@code int}.
* <p>
* The leading plus/minus sign, and negative values for other units are
* not part of the ISO-8601 standard. In addition, ISO-8601 does not
* permit mixing between the {@code PnYnMnD} and {@code PnW} formats.
* Any week-based input is multiplied by 7 and treated as a number of days.
* <p>
* For example, the following are valid inputs:
* <pre>
* "P2Y" -- Period.ofYears(2)
* "P3M" -- Period.ofMonths(3)
* "P4W" -- Period.ofWeeks(4)
* "P5D" -- Period.ofDays(5)
* "P1Y2M3D" -- Period.of(1, 2, 3)
* "P1Y2M3W4D" -- Period.of(1, 2, 25)
* "P-1Y2M" -- Period.of(-1, 2, 0)
* "-P1Y2M" -- Period.of(-1, -2, 0)
* </pre>
*
* @param text the text to parse, not null
* @return the parsed period, not null
* @throws DateTimeParseException if the text cannot be parsed to a period
*/
public static Period parse(CharSequence text) {
// 其他代码
}
// 其他代码
}
获得年月日
period.getYears();
period.getMonths();
period.getDays();
比较方法
用between来比较日期。
LocalDate startDate = LocalDate.of(2015, 2, 20);
LocalDate endDate = LocalDate.of(2017, 1, 15);
// startDate减endDate
Period period = Period.between(startDate, endDate);
// 任何一个时间单元为负数,则返回true。true:endDate早于startDate
period.isNegative()
增减方法
Period period = Period.parse("P2Y3M5D");
period.plusDays(50);
period.minusMonths(2);
转换单位
Period period = Period.parse("P1Y2M3D");
period.toTotalMonths(); // 14
取值方法
Period period = Period.parse("P1Y2M3D");
period.getYears(); // 1
period.getMonths(); // 2
period.getDays(); // 3
来源:https://knife.blog.csdn.net/article/details/122772818
标签:Java,Period类
0
投稿
猜你喜欢
idea向System.getenv()添加系统环境变量的操作
2022-11-13 19:35:51
c# 抓取Web网页数据分析
2022-11-10 06:44:57
ES结合java代码聚合查询详细示例
2022-08-31 01:23:29
Java Swing JFrame窗口的实现
2021-11-30 12:03:37
java多线程创建及线程安全详解
2022-12-02 18:58:51
C#控制台程序如何发布到服务器Linux上运行
2022-07-17 05:57:52
Android中ScrollView嵌套GridView的解决办法
2021-07-12 09:37:06
Java动态数组Arraylist存放自定义数据类型方式
2023-07-25 10:41:30
ListView实现下拉动态渲染数据
2022-10-31 11:19:12
c#实现一个超实用的证件照换底色小工具(附源码)
2023-05-20 08:32:17
猜数游戏java实现代码
2023-01-05 18:39:28
Android Studio进行APP图标更改的两种方式总结
2022-02-07 09:08:40
通过LinQ查询字符出现次数的实例方法
2023-12-10 13:40:01
JAVA.io读写文件方式汇总
2022-10-14 06:32:21
java ssm框架实现分页功能的示例代码(oracle)
2021-10-31 01:14:40
C#中ref关键字的用法
2022-07-17 21:30:47
java WSDL接口webService实现方式
2022-06-15 21:03:29
用Newtonsoft将json串转为对象的方法(详解)
2022-12-31 23:32:21
Android webview旋转屏幕导致页面重新加载问题解决办法
2021-10-18 17:07:35
Android无需读写权限通过临时授权读写用户文件详解
2022-11-05 12:50:54