Java Date from() Method



Description

The Java Date from(Instant instant) returns a date instance from an Instant object. Instant uses a higher precision of nanoseconds, whereas Date uses a precision of milliseconds. During conversion any excess precision information is truncated. Instant can store points on the time-line further in the future and further in the past than Date and such scenario will throw an exception.

Declaration

Following is the declaration for java.util.Date.from(Instant instant) method

public static Date from​(Instant instant)

Parameters

instant − the instant to convert.

Return Value

This method returns a Date representing the same point on the time-line as the provided instant.

Exception

NullPointerException − if instant is null.

IllegalArgumentException − if the instant is too large to represent as a Date.

Creating Date Instance from Instant.now() Method Example

The following example shows the usage of Java Date from() method. We're creating a Date instance of current time using Instant using from() method.

package com.tutorialspoint;

import java.time.Instant;
import java.util.Date;

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

      // create a date
      Date date = Date.from(Instant.now());

      System.out.println("Date: " + date.toString());
   }
}

Output

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

Date: Mon Apr 01 12:10:07 IST 2024

Creating Date Instance from Instant.MAX property Example

The following example shows the usage of Java Date from() method. We're creating a Date instance of current time using Instant.MAX using from() method. Exception will occur as Instant is too large to convert into date.

package com.tutorialspoint;

import java.time.Instant;
import java.util.Date;

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

      // create a date
      Date date = Date.from(Instant.MAX);

      System.out.println("Date: " + date.toString());
   }
}

Output

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

Exception in thread "main" java.lang.IllegalArgumentException: java.lang.ArithmeticException: long overflow
	at java.base/java.util.Date.from(Date.java:1362)
	at com.tutorialspoint.DateDemo.main(DateDemo.java:10)
Caused by: java.lang.ArithmeticException: long overflow
	at java.base/java.lang.Math.multiplyExact(Math.java:946)
	at java.base/java.lang.Math.multiplyExact(Math.java:922)
	at java.base/java.time.Instant.toEpochMilli(Instant.java:1236)
	at java.base/java.util.Date.from(Date.java:1360)
	... 1 more

Comparing Date Instance with Instant.now() time Example

The following example shows the usage of Java Date from() method. We're creating a Date instance of current time using Instant using from() method. Here date object truncates the nanoseconds.

package com.tutorialspoint;

import java.time.Instant;
import java.util.Date;

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

      // create a date
      Date date = Date.from(Instant.now());
      
      System.out.println(Instant.now());

      System.out.println("Date: " + date.toString());
   }
}

Output

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

2024-04-01T06:39:42.885311400Z
Date: Mon Apr 01 12:09:42 IST 2024
java_util_date.htm
Advertisements