java.time.Instant.truncatedTo() Method Example



Description

The java.time.Instant.truncatedTo(TemporalUnit unit) method returns a copy of this Instant truncated to the specified unit.

Declaration

Following is the declaration for java.time.Instant.truncatedTo(TemporalUnit unit) method.

public Instant truncatedTo(TemporalUnit unit)

Parameters

unit − the unit to truncate to, not null.

Return Value

an Instant based on this instant with the time truncated, not null.

Exceptions

  • DateTimeException − if the unit is invalid for truncation.

  • UnsupportedTemporalTypeException − if the unit is not supported.

Example

The following example shows the usage of java.time.Instant.truncatedTo(TemporalUnit unit) method.

package com.tutorialspoint;

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

public class InstantDemo {
   public static void main(String[] args) {

      Instant instant = Instant.parse("2014-12-03T10:15:30.00Z");
      System.out.println(instant.truncatedTo(ChronoUnit.HOURS));
   }
}

Let us compile and run the above program, this will produce the following result −

2014-12-03T10:00:00Z
Advertisements