java Duration,Duration类

  java Duration,Duration类

  00-1010通过LocalDateTime或LocalTime通过现有持续时间分析方法通过时间单位创建持续时间和期间创建方法介绍使用说明详细比较法增减法转换单位值法

  00-1010本文通过实例介绍java的Duration用法。

  

目录

说明

 

  Duration类通过结合秒和纳秒来描述时间量,最高精度是纳秒。数量可以是正的,也可以是负的,如1天(86400秒0纳秒)、-1天(-86400秒0纳秒)、1年(31556952秒0纳秒)、1毫秒(0秒1,000,000纳秒)等。

  Period类通过结合年、月和日来描述时间量,最高精度是天。数量可以是正数,也可以是负数,比如2年(2年0个月0天)、3个月(0年3个月0天)、4天(0年0个月0天)。

  这两个类是不可变的、线程安全的和最终的。都是JDK8的新品。

  Period用法

  参见:详细解释Java中Period类的用法。

  

简介

 

  00-1010是根据日、小时、分钟、秒和纳秒创建的。

  ofDays()、ofHours()、ofMillis()、ofMinutes()、ofNanos()、ofSeconds().例如:

  duration from days=duration . of days(1);

  00-1010使用LocalDateTime或LocalTime类创建持续时间,然后使用between获取它。

  local datetime start=local datetime . of(2022,1,1,8,0,0);local datetime end=local datetime . of(2022,1,2,8,30,30);duration duration=duration . between(start,end);

  

Duration和Period

du1=Duration . of hours(10);duration duration=duration . from(du1);

 

  

创建方法

 

  

通过时间单位创建

用法示例

 

  duration from char 1=duration . parse( P1 dt 1 h10 m 10.5s );duration from char 2=duration . parse( PT10M );格式说明

  采用ISO-8601时间格式。格式为:PnYnMnDTnHnMnS (n是数字)

  例如p 1y 2m 10 dt 30m 15.03s

  p:开始标记

  y:一年。

  2M:两个月。

  10D:十天

  t:日期和时间的分割标记

  2H:两个小时。

  30M:三十分钟。

  15秒15.02秒

  00-1010 1.p , D , H , M 和 S 可以是大写或小写(建议大写)

  2.负数可以用“-”来表示

  例子大全

  PT20.345S -解析为 20.345秒 PT15M -解析为 15分钟(其中一分钟为60秒) PT10H -解析为 10小时(其中一小时为3600秒) P2D -解析为 2天(其中一天为24小时或86400秒) P2D

  T3H4M" -- 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"

  源码:

  

public final class Duration implements TemporalAmount, Comparable<Duration>, Serializable {//其他代码 //----------------------------------------------------------------------- /** * Obtains a {@code Duration} from a text string such as {@code PnDTnHnMn.nS}. * <p> * This will parse a textual representation of a duration, including the * string produced by {@code toString()}. The formats accepted are based * on the ISO-8601 duration format {@code PnDTnHnMn.nS} with days * considered to be exactly 24 hours. * <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. * The sections have suffixes in ASCII of "D", "H", "M" and "S" for * days, hours, minutes and seconds, accepted in upper or lower case. * The suffixes must occur in order. The ASCII letter "T" must occur before * the first occurrence, if any, of an hour, minute or second section. * At least one of the four sections must be present, and if "T" is present * there must be at least one section after the "T". * The number part of each section must consist of one or more ASCII digits. * The number may be prefixed by the ASCII negative or positive symbol. * The number of days, hours and minutes must parse to an {@code long}. * The number of seconds must parse to an {@code long} with optional fraction. * The decimal point may be either a dot or a comma. * The fractional part may have from zero to 9 digits. * <p> * The leading plus/minus sign, and negative values for other units are * not part of the ISO-8601 standard. * <p> * Examples: * <pre> * "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 or 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" * </pre> * * @param text the text to parse, not null * @return the parsed duration, not null * @throws DateTimeParseException if the text cannot be parsed to a duration */ public static Duration parse(CharSequence text) {......}}

 

  

比较方法

比较两个时间的差

 

  

Instant start = Instant.parse("2017-10-03T10:15:30.00Z");Instant end = Instant.parse("2017-10-03T10:16:30.00Z"); // start - end Duration duration = Duration.between(start, end); // 任何一个时间单元为负数,则返回true。true:end早于startduration.isNegative(); Duration.between(start, end).getSeconds();Duration.between(start, end).getNano();

 

  

增减方法

plusX()、minusX()

 

  X表示days, hours, millis, minutes, nanos 或 seconds

  

Duration duration = Duration.ofHours(2);Duration newDuration = duration.plusSeconds(33);

plus()/minus()方法

 

  带TemporalUnit 类型参数进行加减:

  

Duration duration = Duration.ofHours(2);Duration newDuration = duration.plus(33, ChronoUnit.SECONDS);

 

  

转换单位

可以用toX来转换为其他单位,支持:toDays, toHours, toMinutes, toMillis, toNanos

 

  

Duration duration = Duration.ofHours(2); duration.toDays(); // 0duration.toHours(); // 2duration.toMinutes(); // 120duration.toMillis(); // 7200000duration.toNanos(); // 7200000000000

 

  

取值方法

可以用getX来获得指定位置的值,因为Duration是由秒和纳秒组成,所以只能获得秒和纳秒:

 

  

Duration duration = Duration.ofHours(2); duration.getSeconds(); //7200duration.getNano(); //

以上就是详解Java中Duration类的使用方法的详细内容,更多关于Java Duration类的资料请关注盛行IT其它相关文章!

 

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: