Close

Java Date Time - Instant.minus() Examples

Java Date Time Java Java API 


Class:

java.time.Instant

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

Methods:

public Instant minus(TemporalAmount amountToSubtract)

Returns a copy of this instant with the specified amount subtracted.


Examples


package com.logicbig.example.instant;

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

public class MinusExample {

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

for (ChronoUnit unit : ChronoUnit.values()) {
try {
Instant v = i.minus(10, unit);
System.out.printf("%8s > %s%n", unit, v);
} catch (UnsupportedTemporalTypeException e) {
System.out.printf(" -- %s not supported%n", unit);
}
}
}
}

Output

2017-05-01T20:57:43.045Z
Nanos > 2017-05-01T20:57:43.044999990Z
Micros > 2017-05-01T20:57:43.044990Z
Millis > 2017-05-01T20:57:43.035Z
Seconds > 2017-05-01T20:57:33.045Z
Minutes > 2017-05-01T20:47:43.045Z
Hours > 2017-05-01T10:57:43.045Z
HalfDays > 2017-04-26T20:57:43.045Z
Days > 2017-04-21T20:57:43.045Z
-- Weeks not supported
-- Months not supported
-- Years not supported
-- Decades not supported
-- Centuries not supported
-- Millennia not supported
-- Eras not supported
-- Forever not supported




See Also