Java Utility Library

Java Date - from() Method



The java.util.Date.from() method is used to obtain an instance of Date from an Instant object.

Syntax

public static Date from(Instant instant)

Parameters

instant Specify the instant to convert.

Return Value

Returns a Date representing the same point on the time-line as the provided instant.

Exception

  • Throws NullPointerException, if instant is null.
  • Throws IllegalArgumentException, if the instant is too large to represent as a Date

Example:

In the example below, the java.util.Date.from() method is used to get an instance of Date from a given Instant object.

import java.util.*;
import java.time.Instant; 

public class MyClass {
  public static void main(String[] args) {
    //creating a Date object
    Date Dt = new Date();

    //creating an Instant object
    Instant Inst = Instant.now();

    //printing the instant object
    System.out.println("The Instant is: " + Dt.from(Inst));
  }
}

The output of the above code will be:

The Instant is: Sat May 08 11:06:02 UTC 2021

❮ Java.util - Date