Close

Java Date Time - Period.subtractFrom() Examples

Java Date Time Java Java API 


Class:

java.time.Period

java.lang.Objectjava.lang.Objectjava.time.Periodjava.time.Periodjava.time.chrono.ChronoPeriodChronoPeriodjava.io.SerializableSerializableLogicBig

Method:

public Temporal subtractFrom(Temporal temporal)

This method subtracts this period from the specified temporal object and returns the same type of temporal object as of the input temporal type.


Examples


package com.logicbig.example.period;

import java.time.*;
import java.time.temporal.Temporal;

public class SubtractFromExample {

public static void main(String... args) {
Period p = Period.of(10, 2, 13);
System.out.println(p);

subtract(LocalDate.now(), p);
subtract(LocalDateTime.now(), p);
subtract(OffsetDateTime.now(), p);
subtract(ZonedDateTime.now(), p);
}

private static void subtract(Temporal t, Period p) {
Temporal t2 = p.subtractFrom(t);
String s = t2.getClass().getSimpleName();
System.out.printf("%15s > %s > %s%n", s, t, t2);
}
}

Output

P10Y2M13D
LocalDate > 2017-05-01 > 2007-02-16
LocalDateTime > 2017-05-01T15:55:37.034 > 2007-02-16T15:55:37.034
OffsetDateTime > 2017-05-01T15:55:37.034-05:00 > 2007-02-16T15:55:37.034-05:00
ZonedDateTime > 2017-05-01T15:55:37.035-05:00[America/Chicago] > 2007-02-16T15:55:37.035-06:00[America/Chicago]




See Also