亲宝软件园·资讯

展开

Java Duration类

IT利刃出鞘 人气:0

简介

本文用示例介绍java的Duration的用法。

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新增的。

Period用法

见:详解Java中Period类的使用方法

创建方法

通过时间单位创建

基于天、时、分、秒、纳秒创建。

ofDays(), ofHours(), ofMillis(), ofMinutes(), ofNanos(), ofSeconds()。例如:

Duration fromDays = Duration.ofDays(1);

通过LocalDateTime或LocalTime

通过LocalDateTime或者LocalTime 类,然后使用between获取创建Duration。

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

通过已有的Duration

Duration du1 = Duration.ofHours(10);
Duration duration = Duration.from(du1);

解析方法

用法说明

用法示例

Duration fromChar1 = Duration.parse("P1DT1H10M10.5S");
Duration fromChar2 = Duration.parse("PT10M");

格式说明

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

例如:P1Y2M10DT2H30M15.03S

P:开始标记

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

源码:

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早于start
duration.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();     // 0
duration.toHours();    // 2
duration.toMinutes();  // 120
duration.toMillis();   // 7200000
duration.toNanos();    // 7200000000000

取值方法

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

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

加载全部内容

相关教程
猜你喜欢
用户评论