Java Get Current Date 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
import java.time.LocalDate;
public class GetCurrentDate {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println(now.toString());
}
}
Output:
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:
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:
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()
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:
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
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")));
}
}
Output:
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:
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:
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
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:
2020/02/08 - 00:18:12
Day of month: 8
Current hour: 0