Java Get Current Date Time

Amir
Amir
Java Get Current Date Time

Java get current date and time

How to get the current date and time in Java? In this tutorial we’ll look at three different methods in Java 8.

The classes used to display the date and time are LocalDate, LocalTime, LocalDateTime.

Get Current Date

The LocalDate class is used to represent a date.

GetCurrentDate.java

1
2
3
4
5
6
7
8
9
10
11
package io.devqa.tutorials;

import java.time.LocalDate;

public class GetCurrentDate {

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        System.out.println(now.toString());
    }
}

Output:

1
2020-02-07

Formatted Date

We can use the DateTimeFormatter class to format the display of the date. For example to display the current date in the format of yyyy/mm/dd we use:

1
2
3
4
5
6
7
8
9
10
11
12
package io.devqa.tutorials;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class GetCurrentDate {

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
    }
}

Output:

1
2020/02/07

LocalDate class has other useful methods that we can use to get more detail about the current date such as: getDayOfWeek(), getDayOfMonth(), getDayOfYear()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package io.devqa.tutorials;

import java.time.LocalDate;

public class GetCurrentDate {

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        System.out.println("Today's date: " + now.toString());
        System.out.println("Day of week: " + now.getDayOfWeek().toString());
        System.out.println("Day of month: " + now.getDayOfMonth());
        System.out.println("Day of year: " + now.getDayOfYear());
    }
}

Output:

1
2
3
4
Today's date: 2020-02-07
Day of week: FRIDAY
Day of month: 7
Day of year: 38

Get Current Time

The LocalTime class represents a time.

GetCurrentTime.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package io.devqa.tutorials;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class GetCurrentTime {

    public static void main(String[] args) {
        LocalTime now = LocalTime.now();
        System.out.println("Time now: " + now.toString());
        System.out.println("Formatted time: " + now.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
    }
}

Note We can use DateTimeFormatter to format the display of the time.

Output:

1
2
Time now: 00:02:53.313
Formatted time: 00:02:53

LocalTime class also has some handy utility methods to get more detail about the current time:

1
2
3
4
5
6
7
8
9
10
11
12
13
package io.devqa.tutorials;

import java.time.LocalTime;

public class GetCurrentTime {

    public static void main(String[] args) {
        LocalTime now = LocalTime.now();
        System.out.println("Current hour: " + now.getHour());
        System.out.println("Current minute: " + now.getMinute());
        System.out.println("Current second: " + now.getSecond());
    }
}

Output:

1
2
3
Current hour: 0
Current minute: 10
Current second: 16

Get Current Date Time

To get the current date and time, we can use the LocalDateTime class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package io.devqa.tutorials;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class GetCurrentTime {

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd - HH:mm:ss")));
        System.out.println("Day of month: " + now.getDayOfMonth());
        System.out.println("Current hour: " + now.getHour());
    }
}

Output:

1
2
3
2020/02/08 - 00:18:12
Day of month: 8
Current hour: 0
See also