Java Get Current Date and Time

Tags: LocalDateTime Date Java Date System.currentTimeMillis current date time

In this Java tutorial we learn how to get the current date and time in Java with different solutions.

Using System.currentTimeMillis() method

The following Java program to show you how to use the System.currentTimeMillis() method to get current time as a milliseconds value.

CurrentTimeMillisExample1.java

public class CurrentTimeMillisExample1 {

    public static void main(String... args) {
        long current = System.currentTimeMillis();

        System.out.println(current);
    }

}
The output as below.
1642095378764

Create Date object from current time

The Java program below we create Date object as current time.

CurrentTimeMillisExample2.java

import java.util.Date;

public class CurrentTimeMillisExample2 {

    public static void main(String... args) {

        Date date = new Date(System.currentTimeMillis());

        System.out.println(date);
    }
}
The output as below.
Fri Jan 14 00:37:48 ICT 2022

Using LocalDateTime.now() method

In the Java program below we show you how to use the LocalDateTime.now() method to create LocalDateTime object as current date time.

DateTimeExample1.java

import java.time.LocalDateTime;

public class DateTimeExample1 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDateTime);
    }
}
The output as below.
2022-02-09T23:31:07.961

Happy Coding 😊

Java Date by Examples

Java Calendar using Calendar.Builder by Examples

Java Calendar by Examples