Close

Java Date Time - Instant.truncatedTo() 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

Method:

public Instant truncatedTo(TemporalUnit unit)
Returns a copy of this Instant truncated to the specified unit.

Examples


package com.logicbig.example.instant;

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

public class TruncatedToExample {

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

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


}
}

Output

2017-05-01T20:57:03.801Z
Nanos > 2017-05-01T20:57:03.801Z
Micros > 2017-05-01T20:57:03.801Z
Millis > 2017-05-01T20:57:03.801Z
Seconds > 2017-05-01T20:57:03Z
Minutes > 2017-05-01T20:57:00Z
Hours > 2017-05-01T20:00:00Z
HalfDays > 2017-05-01T12:00:00Z
Days > 2017-05-01T00:00:00Z
--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