Close

Java Date Time - Year.minus() Examples

Java Date Time Java Java API 


Class:

java.time.Year

java.lang.Objectjava.lang.Objectjava.time.Yearjava.time.Yearjava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Methods:

public Year minus(TemporalAmount amountToSubtract)

Returns a copy of this year with the specified temporal amount subtracted.



public Year minus(long amountToSubtract,
                  TemporalUnit unit)

Returns a copy of this year with the specified amount for the specified temporal unit subtracted.


Examples


package com.logicbig.example.year;

import java.time.Period;
import java.time.Year;

public class MinusExample {

public static void main(String... args) {
Year y = Year.of(2011);
System.out.println(y);

Period p = Period.ofYears(100);
System.out.println(p);

Year y2 = y.minus(p);
System.out.println(y2);

Period p2 = Period.ofYears(-100);
System.out.println(p2);

Year y3 = y.minus(p2);
System.out.println(y3);
}
}

Output

2011
P100Y
1911
P-100Y
2111




package com.logicbig.example.year;

import java.time.Year;
import java.time.temporal.ChronoUnit;
import java.time.temporal.UnsupportedTemporalTypeException;

public class MinusExample2 {

public static void main(String... args) {
Year d = Year.now();
System.out.println(d);

for (ChronoUnit chronoUnit : ChronoUnit.values()) {
try {
Year d2 = d.minus(1, chronoUnit);
System.out.printf("%10s: %s%n", chronoUnit.name(), d2);
} catch (UnsupportedTemporalTypeException e) {
System.out.printf("%10s: not supported%n", chronoUnit.name());
}
}
}
}

Output

2017
NANOS: not supported
MICROS: not supported
MILLIS: not supported
SECONDS: not supported
MINUTES: not supported
HOURS: not supported
HALF_DAYS: not supported
DAYS: not supported
WEEKS: not supported
MONTHS: not supported
YEARS: 2016
DECADES: 2007
CENTURIES: 1917
MILLENNIA: 1017
ERAS: -2016
FOREVER: not supported




See Also