Close

Java Date Time - YearMonth.minus() Examples

Java Date Time Java Java API 


Class:

java.time.YearMonth

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

Methods:

public YearMonth minus(TemporalAmount amountToSubtract)

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



public YearMonth minus(long amountToSubtract,
                       TemporalUnit unit)

Returns a copy of this year-month with the specified amount of the specified unit subtracted.


Examples


package com.logicbig.example.yearmonth;

import java.time.Period;
import java.time.YearMonth;

public class MinusExample {

public static void main(String... args) {
YearMonth y = YearMonth.of(2010, 11);
System.out.println(y);

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

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

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

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

Output

2010-11
P100Y
1910-11
P-100Y
2110-11




package com.logicbig.example.yearmonth;

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

public class MinusExample2 {

public static void main(String... args) {
YearMonth y = YearMonth.of(2010, 11);
System.out.println(y);

for (ChronoUnit chronoUnit : ChronoUnit.values()) {
try {
YearMonth d2 = y.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

2010-11
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: 2010-10
YEARS: 2009-11
DECADES: 2000-11
CENTURIES: 1910-11
MILLENNIA: 1010-11
ERAS: -2009-11
FOREVER: not supported




See Also