Date toInstant() in Java

toInstant(): This method is available in java.util.Date class of Java.

Syntax:

Instant java.util.Date.toInstant()

This method converts this Date object to an Instant.

Parameters: NA

Returns: an instant representing the same point on the timeline as this Date object.

Exceptions: NA

Approach

Java

import java.util.Date;

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

        Date date = new Date();

        System.out.println(date.toInstant());

    }
}

Output:

2022-01-30T12:49:53.489Z


No comments:

Post a Comment