详解Java中Period类的使用方法
作者:IT利刃出鞘 发布时间:2023-11-28 21:04:44
标签:Java,Period类
简介
本文用示例介绍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


猜你喜欢
- spring profile 多环境配置管理现象 如果在开发时进行一些数据库测试,希望链接到一个测试的数据库,以避免对开发数据
- using System;using System.Collections.Generic;using System.Linq;using
- Java 8新增了LocalDate和LocalTime接口,为什么要搞一套全新的处理日期和时间的API?因为旧的java.util.Dat
- 本文实例讲述了android中图形图像处理之drawable用法。分享给大家供大家参考。具体如下:一、如何获取 res 中的资源数据包pac
- 实现效果:奔溃的线程侠:(单线程)主线程正在处理刷新图片的请求时,无法再接受其他请求,从而陷入阻塞的死循环状态。绘制图片import jav
- 加锁和解锁我们来看下ReentrantLock的基本用法ThreadDomain35类public class ThreadDomain35
- 本文实例为大家分享了Unity实现新手引导镂空效果的具体代码,供大家参考,具体内容如下一、实现思路创建有8个顶点的Mesh,内外边界都是四边
- 废话不多说,先看效果,如果大家感觉不错,请参考实现代码这个功能我是用Fragmentdialog里面做的,也遇到不少坑第一,就是设置背景的d
- 选择排序:(Selection sort)是一种简单直观的排序算法,也是一种不稳定的排序方法。选择排序的原理一组无序待排数组,做升序排序,我
- @PathVariable和@RequestParam传参为空@RestControllerpublic class UserControl
- 介绍在进行项目开发的时候,刚好需要用到对字符串表达式进行求值的处理场景,因此寻找了几个符合要求的第三方组件LambdaParser、Dyna
- Spring Boot @RestController重定向redirectSpring MVC项目中页面重定向一般使用return &qu
- 1. 二叉树的顺序存储1.1 存储方式使用数组保存二叉树结构,方式即将二叉树用 层序遍历 方式放入数组中。一般只适合表示完全二叉树,这种方式
- 引言在前面的内容中,我们先是一一介绍了Collection集合中都有哪些种类的集合,并且详细地讲解了List集合中的相关知识,那么今天我们来
- 我们今天不探讨框架层面的内容,暂且认为90%的框架不存在无法容忍的性能问题。在做系统调优的过程中,面对随处可见的invoke调用,我的内心其
- 前言虽然Android程序是使用Java语言开发的,当然,现在也可以使用kotlin语言。但是实际上我们开发出来的Android程序并不能运
- 写在前面,在笔者完成这个demo的时候,笔者发现现在大家已经不用Ajax来完成联级菜单了,实际上笔者这个demo也并不是为了完成这个,笔者主
- 本文实例讲述了Android编程基于距离传感器控制手机屏幕熄灭的方法。分享给大家供大家参考,具体如下:在现实生活中,打电话的时候手机挨着自己
- 在使用fastJson时,对于泛型的反序列化很多场景下都会使用到TypeReference,例如:void testTypeReferenc
- SpringBoot使用Commons Logging进行所有内部日志记录,但保留底层日志实现。默认提供了Java Util Logging