Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Instant truncatedTo() method in Java with Examples

  • Last Updated : 29 Nov, 2018

The truncatedTo() method of an Instant class is used to get the value of this Instant in the specified unit. This method takes a parameter Unit, which is the Unit in which this Instant is to be truncated to. It returns a truncated immutable Instant with the value in the specified unit.

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

public Instant truncatedTo(TemporalUnit unit)

Parameter: This method takes a parameter unit which is the unit in which this Instant is to be truncated to. It should not be null.

Returns: This method returns an immutable truncated Instant with the value in the specified unit.



Exception: This method throws following Exceptions:

  • DateTimeException: if the unit is invalid for truncation.
  • UnsupportedTemporalTypeException: if the unit is not supported.

Below programs illustrate the Instant.truncatedTo() method:

Program 1:




// Java program to demonstrate
// Instant.truncatedTo() method
  
import java.time.*;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T09:24:54.63Z");
  
        // print instance
        System.out.println("Instant before"
                           + " truncate: "
                           + instant);
  
        // truncate to ChronoUnit.HOURS
        // means unit smaller than Hour
        // will be Zero
        Instant returnvalue
            = instant.truncatedTo(ChronoUnit.HOURS);
  
        // print result
        System.out.println("Instant after "
                           + " truncate: "
                           + returnvalue);
    }
}
Output:
Instant before truncate: 2018-12-30T09:24:54.630Z
Instant after  truncate: 2018-12-30T09:00:00Z

Program 2:




// Java program to demonstrate
// Instant.truncatedTo() method
  
import java.time.*;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T09:24:54.63Z");
  
        // print instance
        System.out.println("Instant before"
                           + " truncate: "
                           + instant);
  
        // truncate to ChronoUnit.DAYS
        // means unit smaller than DAY
        // will be Zero
        Instant returnvalue
            = instant.truncatedTo(ChronoUnit.DAYS);
  
        // print result
        System.out.println("Instant after "
                           + " truncate: "
                           + returnvalue);
    }
}
Output:
Instant before truncate: 2018-12-30T09:24:54.630Z
Instant after  truncate: 2018-12-30T00:00:00Z

Program 3: To show Exception:




// Java program to demonstrate
// Instant.truncatedTo() method
  
import java.time.*;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T09:24:54.63Z");
  
        try {
            instant.truncatedTo(ChronoUnit.ERAS);
        }
        catch (Exception e) {
  
            // print result
            System.out.println("Exception: " + e);
        }
    }
}
Output:
Exception:
 java.time.temporal.UnsupportedTemporalTypeException:
 Unit is too large to be used for truncation

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#truncatedTo(java.time.temporal.TemporalUnit)




My Personal Notes arrow_drop_up
Recommended Articles
Page :