Java : Duration with Examples
Duration (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Duration methods.
Summary
// 100 seconds
final var duration = Duration.ofSeconds(100);
System.out.println(duration.toSeconds()); // 100
// 100 seconds = 1 minute and 40 seconds
System.out.println(duration.toMinutesPart()); // 1
System.out.println(duration.toSecondsPart()); // 40
// 2000 minutes
final var duration = Duration.ofMinutes(2000);
System.out.println(duration.toMinutes()); // 2000
// 2000 minutes = 1 day and 9 hours and 20 minutes
System.out.println(duration.toDaysPart()); // 1
System.out.println(duration.toHoursPart()); // 9
System.out.println(duration.toMinutesPart()); // 20
Fields
static final Duration ZERO
System.out.println(Duration.ZERO); // PT0S
System.out.println(Duration.ZERO.getSeconds()); // 0
System.out.println(Duration.ZERO.getNano()); // 0
Methods
Duration abs ()
final var duration = Duration.ofSeconds(-100);
System.out.println(duration); // PT-1M-40S
System.out.println(duration.getSeconds()); // -100
final var ret = duration.abs();
System.out.println(ret); // PT1M40S
System.out.println(ret.getSeconds()); // 100
final var duration = Duration.ofDays(1);
System.out.println(duration); // PT24H
System.out.println(duration.getSeconds()); // 86400
final var ret = duration.abs();
System.out.println(ret); // PT24H
System.out.println(ret.getSeconds()); // 86400
Temporal addTo (Temporal temporal)
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var time = LocalTime.of(12, 30);
System.out.println(time); // 12:30
final var ret = duration.addTo(time);
System.out.println(ret); // 12:31:40
final var duration = Duration.ofDays(-1);
System.out.println(duration); // PT-24H
System.out.println(duration.toDays()); // -1
final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00
final var ret = duration.addTo(dateTime);
System.out.println(ret); // 2099-12-31T00:00
static Duration between (Temporal startInclusive, Temporal endExclusive)
final var time1 = LocalTime.of(12, 0);
final var time2 = LocalTime.of(14, 30);
System.out.println(time1); // 12:00
System.out.println(time2); // 14:30
final var duration = Duration.between(time1, time2);
System.out.println(duration); // PT2H30M
System.out.println(duration.toHoursPart()); // 2
System.out.println(duration.toMinutesPart()); // 30
final var time1 = LocalDateTime.of(2100, 1, 1, 0, 0);
final var time2 = LocalDateTime.of(2100, 1, 2, 0, 0);
System.out.println(time1); // 2100-01-01T00:00
System.out.println(time2); // 2100-01-02T00:00
final var duration = Duration.between(time1, time2);
System.out.println(duration); // PT24H
System.out.println(duration.toDaysPart()); // 1
int compareTo (Duration otherDuration)
final var duration1 = Duration.ofSeconds(123);
final var duration2 = Duration.ofSeconds(456);
System.out.println(duration1); // PT2M3S
System.out.println(duration2); // PT7M36S
System.out.println(duration1.compareTo(duration2)); // -1
final var duration1 = Duration.ofSeconds(100);
final var duration2 = Duration.ofSeconds(-100);
System.out.println(duration1); // PT1M40S
System.out.println(duration2); // PT-1M-40S
System.out.println(duration1.compareTo(duration2)); // 1
final var duration1 = Duration.ofSeconds(123);
final var duration2 = Duration.ofSeconds(123, 1);
System.out.println(duration1); // PT2M3S
System.out.println(duration2); // PT2M3.000000001S
System.out.println(duration1.compareTo(duration2)); // -1
Duration dividedBy (long divisor)
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
System.out.println(duration.getNano()); // 0
final var ret1 = duration.dividedBy(2);
System.out.println(ret1); // PT50S
System.out.println(ret1.getSeconds()); // 50
System.out.println(ret1.getNano()); // 0
final var ret2 = duration.dividedBy(3);
System.out.println(ret2); // PT33.333333333S
System.out.println(ret2.getSeconds()); // 33
System.out.println(ret2.getNano()); // 333333333
long dividedBy (Duration divisor)
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var divisor = Duration.ofSeconds(2);
System.out.println(divisor); // PT2S
System.out.println(divisor.getSeconds()); // 2
final var ret = duration.dividedBy(divisor);
System.out.println(ret); // 50
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var divisor = Duration.ofSeconds(3);
System.out.println(divisor); // PT3S
System.out.println(divisor.getSeconds()); // 3
final var ret = duration.dividedBy(divisor);
System.out.println(ret); // 33
boolean equals (Object other)
final var duration1 = Duration.ofSeconds(60);
final var duration2 = Duration.ofMinutes(1);
System.out.println(duration1); // PT1M
System.out.println(duration2); // PT1M
System.out.println(duration1.equals(duration2)); // true
final var duration1 = Duration.ofSeconds(123);
final var duration2 = Duration.ofSeconds(456);
System.out.println(duration1); // PT2M3S
System.out.println(duration2); // PT7M36S
System.out.println(duration1.equals(duration2)); // false
final var duration1 = Duration.ofSeconds(100);
final var duration2 = Duration.ofSeconds(-100);
System.out.println(duration1); // PT1M40S
System.out.println(duration2); // PT-1M-40S
System.out.println(duration1.equals(duration2)); // false
final var duration1 = Duration.ofSeconds(123);
final var duration2 = Duration.ofSeconds(123, 1);
System.out.println(duration1); // PT2M3S
System.out.println(duration2); // PT2M3.000000001S
System.out.println(duration1.equals(duration2)); // false
static Duration from (TemporalAmount amount)
final var amount = Duration.ofSeconds(123);
System.out.println(amount); // PT2M3S
System.out.println(amount.getSeconds()); // 123
final var ret = Duration.from(amount);
System.out.println(ret); // PT2M3S
System.out.println(ret.getSeconds()); // 123
long get (TemporalUnit unit)
final var duration = Duration.ofSeconds(100, 123);
System.out.println(duration); // PT1M40.000000123S
System.out.println(duration.get(ChronoUnit.SECONDS)); // 100
System.out.println(duration.get(ChronoUnit.NANOS)); // 123
final var duration = Duration.ofDays(1);
System.out.println(duration); // PT24H
try {
final var ret = duration.get(ChronoUnit.DAYS);
} catch (UnsupportedTemporalTypeException e) {
System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}
// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported unit: Days
int getNano ()
final var duration = Duration.ofSeconds(100, 123);
System.out.println(duration); // PT1M40.000000123S
System.out.println(duration.getSeconds()); // 100
System.out.println(duration.getNano()); // 123
final var duration = Duration.ofMinutes(1);
System.out.println(duration); // PT1M
System.out.println(duration.getSeconds()); // 60
System.out.println(duration.getNano()); // 0
final var duration = Duration.ofMillis(123456);
System.out.println(duration); // PT2M3.456S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456000000
long getSeconds ()
final var duration = Duration.ofSeconds(100, 123);
System.out.println(duration); // PT1M40.000000123S
System.out.println(duration.getSeconds()); // 100
System.out.println(duration.getNano()); // 123
final var duration = Duration.ofMinutes(1);
System.out.println(duration); // PT1M
System.out.println(duration.getSeconds()); // 60
System.out.println(duration.getNano()); // 0
final var duration = Duration.ofMillis(123456);
System.out.println(duration); // PT2M3.456S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456000000
List<TemporalUnit> getUnits ()
final var duration = Duration.ofSeconds(123);
System.out.println(duration.getUnits()); // [Seconds, Nanos]
int hashCode ()
System.out.println(Duration.ZERO.hashCode()); // 0
System.out.println(Duration.ofSeconds(123).hashCode()); // 123
System.out.println(Duration.ofSeconds(123, 456).hashCode()); // 23379
System.out.println(Duration.ofMinutes(-50).hashCode()); // 2999
boolean isNegative ()
System.out.println(Duration.ZERO.isNegative()); // false
System.out.println(Duration.ofSeconds(123).isNegative()); // false
System.out.println(Duration.ofSeconds(-456).isNegative()); // true
boolean isPositive ()
System.out.println(Duration.ZERO.isPositive()); // false
System.out.println(Duration.ofSeconds(123).isPositive()); // true
System.out.println(Duration.ofSeconds(-456).isPositive()); // false
boolean isZero ()
System.out.println(Duration.ZERO.isZero()); // true
System.out.println(Duration.ofSeconds(123).isZero()); // false
System.out.println(Duration.ofSeconds(-456).isZero()); // false
Duration minus (long amountToSubtract, TemporalUnit unit)
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var ret = duration.minus(20, ChronoUnit.SECONDS);
System.out.println(ret); // PT1M20S
System.out.println(ret.getSeconds()); // 80
final var duration = Duration.ofSeconds(100, 200);
System.out.println(duration); // PT1M40.0000002S
System.out.println(duration.getSeconds()); // 100
System.out.println(duration.getNano()); // 200
final var ret = duration.minus(150, ChronoUnit.NANOS);
System.out.println(ret); // PT1M40.00000005S
System.out.println(ret.getSeconds()); // 100
System.out.println(ret.getNano()); // 50
Duration minus (Duration duration)
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var ret = duration.minus(Duration.ofSeconds(20));
System.out.println(ret); // PT1M20S
System.out.println(ret.getSeconds()); // 80
final var duration = Duration.ofSeconds(100, 200);
System.out.println(duration); // PT1M40.0000002S
System.out.println(duration.getSeconds()); // 100
System.out.println(duration.getNano()); // 200
final var ret = duration.minus(Duration.ofSeconds(0, 150));
System.out.println(ret); // PT1M40.00000005S
System.out.println(ret.getSeconds()); // 100
System.out.println(ret.getNano()); // 50
Duration minusDays (long daysToSubtract)
final var duration = Duration.ofDays(100);
System.out.println(duration); // PT2400H
System.out.println(duration.getSeconds()); // 8640000
System.out.println(duration.toDays()); // 100
final var ret = duration.minusDays(20);
System.out.println(ret); // PT1920H
System.out.println(ret.getSeconds()); // 6912000
System.out.println(ret.toDays()); // 80
final var duration = Duration.ofHours(34);
System.out.println(duration); // PT34H
System.out.println(duration.toDaysPart()); // 1
System.out.println(duration.toHoursPart()); // 10
final var ret = duration.minusDays(3);
System.out.println(ret); // PT-38H
System.out.println(ret.toDaysPart()); // -1
System.out.println(ret.toHoursPart()); // -14
Duration minusHours (long hoursToSubtract)
final var duration = Duration.ofHours(100);
System.out.println(duration); // PT100H
System.out.println(duration.getSeconds()); // 360000
System.out.println(duration.toHours()); // 100
final var ret = duration.minusHours(20);
System.out.println(ret); // PT80H
System.out.println(ret.getSeconds()); // 288000
System.out.println(ret.toHours()); // 80
final var duration = Duration.ofMinutes(80);
System.out.println(duration); // PT1H20M
System.out.println(duration.toHoursPart()); // 1
System.out.println(duration.toMinutesPart()); // 20
final var ret = duration.minusHours(3);
System.out.println(ret); // PT-1H-40M
System.out.println(ret.toHoursPart()); // -1
System.out.println(ret.toMinutesPart()); // -40
Duration minusMillis (long millisToSubtract)
final var duration = Duration.ofMillis(100);
System.out.println(duration); // PT0.1S
System.out.println(duration.getSeconds()); // 0
System.out.println(duration.getNano()); // 100000000
System.out.println(duration.toMillis()); // 100
final var ret = duration.minusMillis(20);
System.out.println(ret); // PT0.08S
System.out.println(ret.getSeconds()); // 0
System.out.println(ret.getNano()); // 80000000
System.out.println(ret.toMillis()); // 80
final var duration = Duration.ofNanos(100000);
System.out.println(duration); // PT0.0001S
System.out.println(duration.getSeconds()); // 0
System.out.println(duration.getNano()); // 100000
final var ret = duration.minusMillis(3);
System.out.println(ret); // PT-0.0029S
System.out.println(ret.getSeconds()); // -1
System.out.println(ret.getNano()); // 997100000
Duration minusMinutes (long minutesToSubtract)
final var duration = Duration.ofMinutes(100);
System.out.println(duration); // PT1H40M
System.out.println(duration.getSeconds()); // 6000
System.out.println(duration.toMinutes()); // 100
final var ret = duration.minusMinutes(20);
System.out.println(ret); // PT1H20M
System.out.println(ret.getSeconds()); // 4800
System.out.println(ret.toMinutes()); // 80
final var duration = Duration.ofSeconds(80);
System.out.println(duration); // PT1M20S
System.out.println(duration.toMinutesPart()); // 1
System.out.println(duration.toSecondsPart()); // 20
final var ret = duration.minusMinutes(3);
System.out.println(ret); // PT-1H-40M
System.out.println(ret.toMinutesPart()); // -1
System.out.println(ret.toSecondsPart()); // -40
Duration minusNanos (long nanosToSubtract)
final var duration = Duration.ofNanos(100);
System.out.println(duration); // PT0.0000001S
System.out.println(duration.getSeconds()); // 0
System.out.println(duration.getNano()); // 100
final var ret = duration.minusNanos(20);
System.out.println(ret); // PT0.00000008S
System.out.println(ret.getSeconds()); // 0
System.out.println(ret.getNano()); // 80
final var duration = Duration.ofNanos(100);
System.out.println(duration); // PT0.0000001S
System.out.println(duration.getSeconds()); // 0
System.out.println(duration.getNano()); // 100
final var ret = duration.minusNanos(300);
System.out.println(ret); // PT-0.0000002S
System.out.println(ret.getSeconds()); // -1
System.out.println(ret.getNano()); // 999999800
Duration minusSeconds (long secondsToSubtract)
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var ret = duration.minusSeconds(20);
System.out.println(ret); // PT1M20S
System.out.println(ret.getSeconds()); // 80
final var duration = Duration.ofMillis(100);
System.out.println(duration); // PT0.1S
System.out.println(duration.getSeconds()); // 0
System.out.println(duration.getNano()); // 100000000
final var ret = duration.minusSeconds(3);
System.out.println(ret); // PT-2.9S
System.out.println(ret.getSeconds()); // -3
System.out.println(ret.getNano()); // 100000000
Duration multipliedBy (long multiplicand)
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var ret = duration.multipliedBy(2);
System.out.println(ret); // PT3M20S
System.out.println(ret.getSeconds()); // 200
final var duration = Duration.ofSeconds(100, 123);
System.out.println(duration); // PT1M40.000000123S
System.out.println(duration.getSeconds()); // 100
System.out.println(duration.getNano()); // 123
final var ret = duration.multipliedBy(3);
System.out.println(ret); // PT5M0.000000369S
System.out.println(ret.getSeconds()); // 300
System.out.println(ret.getNano()); // 369
Duration negated ()
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var ret = duration.negated();
System.out.println(ret); // PT-1M-40S
System.out.println(ret.getSeconds()); // -100
final var duration = Duration.ofSeconds(100, 123);
System.out.println(duration); // PT1M40.000000123S
System.out.println(duration.getSeconds()); // 100
System.out.println(duration.getNano()); // 123
final var ret = duration.negated();
System.out.println(ret); // PT-1M-40.000000123S
System.out.println(ret.getSeconds()); // -101
System.out.println(ret.getNano()); // 999999877
static Duration of (long amount, TemporalUnit unit)
final var duration = Duration.of(100, ChronoUnit.SECONDS);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
System.out.println(duration.getNano()); // 0
final var duration = Duration.of(123, ChronoUnit.NANOS);
System.out.println(duration); // PT0.000000123S
System.out.println(duration.getSeconds()); // 0
System.out.println(duration.getNano()); // 123
static Duration ofDays (long days)
final var duration = Duration.ofDays(2);
System.out.println(duration); // PT48H
System.out.println(duration.getSeconds()); // 172800
System.out.println(duration.getNano()); // 0
System.out.println(duration.toDays()); // 2
System.out.println(duration.toHours()); // 48
System.out.println(duration.toMinutes()); // 2880
static Duration ofHours (long hours)
final var duration = Duration.ofHours(2);
System.out.println(duration); // PT2H
System.out.println(duration.getSeconds()); // 7200
System.out.println(duration.getNano()); // 0
System.out.println(duration.toHours()); // 2
System.out.println(duration.toMinutes()); // 120
static Duration ofMillis (long millis)
final var duration = Duration.ofMillis(200);
System.out.println(duration); // PT0.2S
System.out.println(duration.getSeconds()); // 0
System.out.println(duration.getNano()); // 200000000
System.out.println(duration.toMillis()); // 200
static Duration ofMinutes (long minutes)
final var duration = Duration.ofMinutes(2);
System.out.println(duration); // PT2M
System.out.println(duration.getSeconds()); // 120
System.out.println(duration.getNano()); // 0
System.out.println(duration.toMinutes()); // 2
static Duration ofNanos (long nanos)
final var duration = Duration.ofNanos(123);
System.out.println(duration); // PT0.000000123S
System.out.println(duration.getSeconds()); // 0
System.out.println(duration.getNano()); // 123
static Duration ofSeconds (long seconds)
final var duration = Duration.ofSeconds(123);
System.out.println(duration); // PT2M3S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 0
static Duration ofSeconds (long seconds, long nanoAdjustment)
final var duration = Duration.ofSeconds(123, 456);
System.out.println(duration); // PT2M3.000000456S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456
static Duration parse (CharSequence text)
final var duration = Duration.parse("PT20.345S");
System.out.println(duration); // PT20.345S
System.out.println(duration.getSeconds()); // 20
System.out.println(duration.getNano()); // 345000000
System.out.println(duration.toMillisPart()); // 345
final var duration = Duration.parse("P2DT3H4M");
System.out.println(duration); // PT51H4M
System.out.println(duration.getSeconds()); // 183840
System.out.println(duration.getNano()); // 0
System.out.println(duration.toDaysPart()); // 2
System.out.println(duration.toHoursPart()); // 3
System.out.println(duration.toMinutesPart()); // 4
Duration plus (long amountToAdd, TemporalUnit unit)
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var ret = duration.plus(20, ChronoUnit.SECONDS);
System.out.println(ret); // PT2M
System.out.println(ret.getSeconds()); // 120
final var duration = Duration.ofSeconds(100, 200);
System.out.println(duration); // PT1M40.0000002S
System.out.println(duration.getSeconds()); // 100
System.out.println(duration.getNano()); // 200
final var ret = duration.plus(150, ChronoUnit.NANOS);
System.out.println(ret); // PT1M40.00000035S
System.out.println(ret.getSeconds()); // 100
System.out.println(ret.getNano()); // 350
Duration plus (Duration duration)
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var ret = duration.plus(Duration.ofSeconds(20));
System.out.println(ret); // PT2M
System.out.println(ret.getSeconds()); // 120
final var duration = Duration.ofSeconds(100, 200);
System.out.println(duration); // PT1M40.0000002S
System.out.println(duration.getSeconds()); // 100
System.out.println(duration.getNano()); // 200
final var ret = duration.plus(Duration.ofSeconds(0, 150));
System.out.println(ret); // PT1M40.00000035S
System.out.println(ret.getSeconds()); // 100
System.out.println(ret.getNano()); // 350
Duration plusDays (long daysToAdd)
final var duration = Duration.ofDays(100);
System.out.println(duration); // PT2400H
System.out.println(duration.getSeconds()); // 8640000
System.out.println(duration.toDays()); // 100
final var ret = duration.plusDays(20);
System.out.println(ret); // PT2880H
System.out.println(ret.getSeconds()); // 10368000
System.out.println(ret.toDays()); // 120
final var duration = Duration.ofHours(-34);
System.out.println(duration); // PT-34H
System.out.println(duration.toDaysPart()); // -1
System.out.println(duration.toHoursPart()); // -10
final var ret = duration.plusDays(3);
System.out.println(ret); // PT38H
System.out.println(ret.toDaysPart()); // 1
System.out.println(ret.toHoursPart()); // 14
Duration plusHours (long hoursToAdd)
final var duration = Duration.ofHours(100);
System.out.println(duration); // PT100H
System.out.println(duration.getSeconds()); // 360000
System.out.println(duration.toHours()); // 100
final var ret = duration.plusHours(20);
System.out.println(ret); // PT120H
System.out.println(ret.getSeconds()); // 432000
System.out.println(ret.toHours()); // 120
final var duration = Duration.ofMinutes(-80);
System.out.println(duration); // PT-1H-20M
System.out.println(duration.toHoursPart()); // -1
System.out.println(duration.toMinutesPart()); // -20
final var ret = duration.plusHours(3);
System.out.println(ret); // PT1H40M
System.out.println(ret.toHoursPart()); // 1
System.out.println(ret.toMinutesPart()); // 40
Duration plusMillis (long millisToAdd)
final var duration = Duration.ofMillis(100);
System.out.println(duration); // PT0.1S
System.out.println(duration.getSeconds()); // 0
System.out.println(duration.getNano()); // 100000000
System.out.println(duration.toMillis()); // 100
final var ret = duration.plusMillis(20);
System.out.println(ret); // PT0.12S
System.out.println(ret.getSeconds()); // 0
System.out.println(ret.getNano()); // 120000000
System.out.println(ret.toMillis()); // 120
final var duration = Duration.ofSeconds(-1, 900000000);
System.out.println(duration); // PT-0.1S
System.out.println(duration.getSeconds()); // -1
System.out.println(duration.getNano()); // 900000000
final var ret = duration.plusMillis(300);
System.out.println(ret); // PT0.2S
System.out.println(ret.getSeconds()); // 0
System.out.println(ret.getNano()); // 200000000
Duration plusMinutes (long minutesToAdd)
final var duration = Duration.ofMinutes(100);
System.out.println(duration); // PT1H40M
System.out.println(duration.getSeconds()); // 6000
System.out.println(duration.toMinutes()); // 100
final var ret = duration.plusMinutes(20);
System.out.println(ret); // PT2H
System.out.println(ret.getSeconds()); // 7200
System.out.println(ret.toMinutes()); // 120
final var duration = Duration.ofSeconds(-80);
System.out.println(duration); // PT-1M-20S
System.out.println(duration.toMinutesPart()); // -1
System.out.println(duration.toSecondsPart()); // -20
final var ret = duration.plusMinutes(3);
System.out.println(ret); // PT1M40S
System.out.println(ret.toMinutesPart()); // 1
System.out.println(ret.toSecondsPart()); // 40
Duration plusNanos (long nanosToAdd)
final var duration = Duration.ofNanos(100);
System.out.println(duration); // PT0.0000001S
System.out.println(duration.getSeconds()); // 0
System.out.println(duration.getNano()); // 100
final var ret = duration.plusNanos(20);
System.out.println(ret); // PT0.00000012S
System.out.println(ret.getSeconds()); // 0
System.out.println(ret.getNano()); // 120
final var duration = Duration.ofSeconds(-1, 999999999);
System.out.println(duration); // PT-0.000000001S
System.out.println(duration.getSeconds()); // -1
System.out.println(duration.getNano()); // 999999999
final var ret = duration.plusNanos(300);
System.out.println(ret); // PT0.000000299S
System.out.println(ret.getSeconds()); // 0
System.out.println(ret.getNano()); // 299
Duration plusSeconds (long secondsToAdd)
final var duration = Duration.ofSeconds(100);
System.out.println(duration); // PT1M40S
System.out.println(duration.getSeconds()); // 100
final var ret = duration.plusSeconds(20);
System.out.println(ret); // PT2M
System.out.println(ret.getSeconds()); // 120
final var duration = Duration.ofMillis(-100);
System.out.println(duration); // PT-0.1S
System.out.println(duration.getSeconds()); // -1
System.out.println(duration.getNano()); // 900000000
final var ret = duration.plusSeconds(3);
System.out.println(ret); // PT2.9S
System.out.println(ret.getSeconds()); // 2
System.out.println(ret.getNano()); // 900000000
Temporal subtractFrom (Temporal temporal)
final var duration = Duration.ofSeconds(30);
System.out.println(duration); // PT30S
System.out.println(duration.getSeconds()); // 30
final var time = LocalTime.of(12, 0);
System.out.println(time); // 12:00
final var ret = duration.subtractFrom(time);
System.out.println(ret); // 11:59:30
long toDays ()
final var duration = Duration.ofDays(2).plusHours(3).plusMinutes(4);
System.out.println(duration); // PT51H4M
System.out.println(duration.getSeconds()); // 183840
System.out.println(duration.toDays()); // 2
System.out.println(duration.toDaysPart()); // 2
System.out.println(duration.toHours()); // 51
System.out.println(duration.toHoursPart()); // 3
System.out.println(duration.toMinutes()); // 3064
System.out.println(duration.toMinutesPart()); // 4
long toDaysPart ()
final var duration = Duration.ofDays(2).plusHours(3).plusMinutes(4);
System.out.println(duration); // PT51H4M
System.out.println(duration.getSeconds()); // 183840
System.out.println(duration.toDays()); // 2
System.out.println(duration.toDaysPart()); // 2
System.out.println(duration.toHours()); // 51
System.out.println(duration.toHoursPart()); // 3
System.out.println(duration.toMinutes()); // 3064
System.out.println(duration.toMinutesPart()); // 4
long toHours ()
final var duration = Duration.ofDays(2).plusHours(3).plusMinutes(4);
System.out.println(duration); // PT51H4M
System.out.println(duration.getSeconds()); // 183840
System.out.println(duration.toDays()); // 2
System.out.println(duration.toDaysPart()); // 2
System.out.println(duration.toHours()); // 51
System.out.println(duration.toHoursPart()); // 3
System.out.println(duration.toMinutes()); // 3064
System.out.println(duration.toMinutesPart()); // 4
int toHoursPart ()
final var duration = Duration.ofDays(2).plusHours(3).plusMinutes(4);
System.out.println(duration); // PT51H4M
System.out.println(duration.getSeconds()); // 183840
System.out.println(duration.toDays()); // 2
System.out.println(duration.toDaysPart()); // 2
System.out.println(duration.toHours()); // 51
System.out.println(duration.toHoursPart()); // 3
System.out.println(duration.toMinutes()); // 3064
System.out.println(duration.toMinutesPart()); // 4
long toMillis ()
final var duration = Duration.ofSeconds(123).plusMillis(456).plusNanos(789);
System.out.println(duration); // PT2M3.456000789S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456000789
System.out.println(duration.toMinutes()); // 2
System.out.println(duration.toMinutesPart()); // 2
System.out.println(duration.toSeconds()); // 123
System.out.println(duration.toSecondsPart()); // 3
System.out.println(duration.toMillis()); // 123456
System.out.println(duration.toMillisPart()); // 456
System.out.println(duration.toNanos()); // 123456000789
System.out.println(duration.toNanosPart()); // 456000789
int toMillisPart ()
final var duration = Duration.ofSeconds(123).plusMillis(456).plusNanos(789);
System.out.println(duration); // PT2M3.456000789S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456000789
System.out.println(duration.toMinutes()); // 2
System.out.println(duration.toMinutesPart()); // 2
System.out.println(duration.toSeconds()); // 123
System.out.println(duration.toSecondsPart()); // 3
System.out.println(duration.toMillis()); // 123456
System.out.println(duration.toMillisPart()); // 456
System.out.println(duration.toNanos()); // 123456000789
System.out.println(duration.toNanosPart()); // 456000789
long toMinutes ()
final var duration = Duration.ofDays(2).plusHours(3).plusMinutes(4);
System.out.println(duration); // PT51H4M
System.out.println(duration.getSeconds()); // 183840
System.out.println(duration.toDays()); // 2
System.out.println(duration.toDaysPart()); // 2
System.out.println(duration.toHours()); // 51
System.out.println(duration.toHoursPart()); // 3
System.out.println(duration.toMinutes()); // 3064
System.out.println(duration.toMinutesPart()); // 4
int toMinutesPart ()
final var duration = Duration.ofDays(2).plusHours(3).plusMinutes(4);
System.out.println(duration); // PT51H4M
System.out.println(duration.getSeconds()); // 183840
System.out.println(duration.toDays()); // 2
System.out.println(duration.toDaysPart()); // 2
System.out.println(duration.toHours()); // 51
System.out.println(duration.toHoursPart()); // 3
System.out.println(duration.toMinutes()); // 3064
System.out.println(duration.toMinutesPart()); // 4
long toNanos ()
final var duration = Duration.ofSeconds(123).plusMillis(456).plusNanos(789);
System.out.println(duration); // PT2M3.456000789S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456000789
System.out.println(duration.toMinutes()); // 2
System.out.println(duration.toMinutesPart()); // 2
System.out.println(duration.toSeconds()); // 123
System.out.println(duration.toSecondsPart()); // 3
System.out.println(duration.toMillis()); // 123456
System.out.println(duration.toMillisPart()); // 456
System.out.println(duration.toNanos()); // 123456000789
System.out.println(duration.toNanosPart()); // 456000789
int toNanosPart ()
final var duration = Duration.ofSeconds(123).plusMillis(456).plusNanos(789);
System.out.println(duration); // PT2M3.456000789S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456000789
System.out.println(duration.toMinutes()); // 2
System.out.println(duration.toMinutesPart()); // 2
System.out.println(duration.toSeconds()); // 123
System.out.println(duration.toSecondsPart()); // 3
System.out.println(duration.toMillis()); // 123456
System.out.println(duration.toMillisPart()); // 456
System.out.println(duration.toNanos()); // 123456000789
System.out.println(duration.toNanosPart()); // 456000789
long toSeconds ()
final var duration = Duration.ofSeconds(123).plusMillis(456).plusNanos(789);
System.out.println(duration); // PT2M3.456000789S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456000789
System.out.println(duration.toMinutes()); // 2
System.out.println(duration.toMinutesPart()); // 2
System.out.println(duration.toSeconds()); // 123
System.out.println(duration.toSecondsPart()); // 3
System.out.println(duration.toMillis()); // 123456
System.out.println(duration.toMillisPart()); // 456
System.out.println(duration.toNanos()); // 123456000789
System.out.println(duration.toNanosPart()); // 456000789
int toSecondsPart ()
final var duration = Duration.ofSeconds(123).plusMillis(456).plusNanos(789);
System.out.println(duration); // PT2M3.456000789S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456000789
System.out.println(duration.toMinutes()); // 2
System.out.println(duration.toMinutesPart()); // 2
System.out.println(duration.toSeconds()); // 123
System.out.println(duration.toSecondsPart()); // 3
System.out.println(duration.toMillis()); // 123456
System.out.println(duration.toMillisPart()); // 456
System.out.println(duration.toNanos()); // 123456000789
System.out.println(duration.toNanosPart()); // 456000789
String toString ()
final var str1 = Duration.ofSeconds(12, 345);
System.out.println(str1); // PT12.000000345S
final var str2 = Duration.ofDays(1).plusHours(2).plusMinutes(3);
System.out.println(str2); // PT26H3M
Duration truncatedTo (TemporalUnit unit)
final var duration = Duration.ofSeconds(123).plusMillis(456).plusNanos(789);
System.out.println(duration); // PT2M3.456000789S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456000789
final var ret1 = duration.truncatedTo(ChronoUnit.MINUTES);
System.out.println(ret1); // PT2M
System.out.println(ret1.getSeconds()); // 120
System.out.println(ret1.getNano()); // 0
final var ret2 = duration.truncatedTo(ChronoUnit.SECONDS);
System.out.println(ret2); // PT2M3S
System.out.println(ret2.getSeconds()); // 123
System.out.println(ret2.getNano()); // 0
final var ret3 = duration.truncatedTo(ChronoUnit.MILLIS);
System.out.println(ret3); // PT2M3.456S
System.out.println(ret3.getSeconds()); // 123
System.out.println(ret3.getNano()); // 456000000
Duration withNanos (int nanoOfSecond)
final var duration = Duration.ofSeconds(123, 456);
System.out.println(duration); // PT2M3.000000456S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456
final var ret = duration.withNanos(789);
System.out.println(ret); // PT2M3.000000789S
System.out.println(ret.getSeconds()); // 123
System.out.println(ret.getNano()); // 789
Duration withSeconds (long seconds)
final var duration = Duration.ofSeconds(123, 456);
System.out.println(duration); // PT2M3.000000456S
System.out.println(duration.getSeconds()); // 123
System.out.println(duration.getNano()); // 456
final var ret = duration.withSeconds(789);
System.out.println(ret); // PT13M9.000000456S
System.out.println(ret.getSeconds()); // 789
System.out.println(ret.getNano()); // 456
Related posts
- Get the current time
- Basics of Date and Time
- Don't use the legacy Date and Calendar classes, use new APIs instead
- API Examples
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- Date
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset