详解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
0
投稿
猜你喜欢
- 相比于直线检测,直线拟合的最大特点是将所有数据只拟合出一条直线void fitLine( InputArray points, Output
- 表单提交这个方法是挺方便的,但在java来说就显得有些麻烦了,怎么个麻烦呢,就是当你字段多的时候,你就得一个一个的获取其对应的值,这样代码量
- Android版本更新实例详解1、导入xutils的jar包 2、在AndroidManifest.xml中添加权限 3、选择下载的路径,和
- 排查@CacheEvict注解失效我简单看了一下《Spring实战》中的demo,然后就应用到业务代码中了,本以为如此简单的事情,竟然在代码
- 概述在移动应用开发中,消息推送可以说是一项非常重要的功能,它能够起到提醒或者唤醒用户的作用,同时也是产品运营人员更高效地实现运营目标的重要手
- 功能:解决web站点的登录,权限验证,授权等功能优点:在不影响站点业务代码,可以权限的授权与验证横切到业务中1、要添加的依赖<!--t
- 1.准备工作第一步就是先要注册一个支付宝的账号(注册这里不说,不是重点),然后登入官方首页,去到应用列表里面找到沙箱应用。基本信息的APPI
- Java main 方法面试题的详细整理1.不用main方法如何定义一个类?不行,没有main方法我们不能运行Java类。在java 7之前
- 缘起工作时使用java开发服务器后台,用Jersey写Restful接口,发现有一个Post方法始终获取不到参数,查了半天,发现时获取参数的
- 简介happens-before是JMM的核心概念。理解happens-before是了解JMM的关键。1、设计意图JMM的设计需要考虑两个
- 一、Java后端使用MultipartFile@PostMapping(value = "/upload")  
- 前言悬浮窗是一种比较常见的需求。例如把视频通话界面缩小成一个悬浮窗,然后用户可以在其他界面上处理事情。本文给出一个简单的应用内悬浮窗实现。可
- 什么是Dozer?Dozer是一种Java Bean到Java Bean的映射器,递归地将数据从一个对象复制到另一个对象,它是一个强大的,通
- 已知字符串“aabbbcddddeeffffghijklmnopqrst”编程找出出现最多的字符和次数,要求时间复杂度小于O(n^2)/**
- 结束firefox的进程,一句代码就够了,如下:Runtime.getRuntime().exec("taskkill /F /I
- 本文是一个 Spring 扩展支持 SPEL 的简单模式,方便第三方通过 Spring 提供额外功能。简化版方式这种方式可以在任何能获取Ap
- 在springboot的开发中,有时候我们会有不同的配置,例如日志打印,数据库连接等,开发,测试,生产每个环境可能配置都不一致,还好,spr
- 在业务开发过程中我们会遇到形形色色的注解,但是框架自有的注解并不是总能满足复杂的业务需求,我们可以自定义注解来满足我们的需求。根据注解使用的
- 新建一个集合List<Bill> billList = new ArrayList<>();将订单中所有物品的名称提
- 资源加载器使用Java,您可以使用当前线程的classLoader并尝试加载文件,但是Spring Framework为您提供了更为优雅的解