Java date and time
In this tutorial, we are going to see how to work with date and time in Java. Java has several classes and packages that allow a programmer to work with date and time.
To getting started with Java date and time, you must have a basic understanding of Java fundamental programming.
Learn Java basics – Getting started in Java Programming
Print current date and time
There are multiple ways to get the current date and time in Java, Try the below-shown examples, it will print the current date and time according to your system default zone. The time might be different if you run the code in our online Java Editor as the code is running on somewhere in the cloud.
LocalDate and LocalTime class
To get the current date we can use the class LocalDate, and to get the current time similarly we have LocalTime class. Following is the simple example that shows how to use LocalDate and LocalTime class to print current date and time –
import java.time.LocalDate; import java.time.LocalTime; public class DateTimeExample1 { public static void main(String[] args) { // Get current date LocalDate currentDate = LocalDate.now(); System.out.println("Current date is " + currentDate); // Get current time LocalTime currentTime = LocalTime.now(); System.out.println("Current time is " + currentTime); } }
LocalDateTime class
The class LocaDateTime allows us to get both date and time. Following is the example program, that shows how you can print current date and time using the class LocaDateTime.
import java.time.LocalDateTime; public class DateTimeExample2 { public static void main(String[] args) { // Get current date and time LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println(currentDateTime); } }
Clock class
Similar to the class LocaDateTime you can also use the class Clock to get the current date and time. Following is the example that shows how you can use class Clock to get the current date and time.
import java.time.Clock; import java.time.Instant; public class DateTimeExample3 { public static void main(String[] args) { // Get current date and time Instant instant = Clock.systemUTC().instant(); System.out.println(instant); } }
Date class
The Java date class available in java.util package can also be used to get current date time. Following is the example that shows how you can use the class Date to get the current date and time.
import java.util.Date; public class DateTimeExample4 { public static void main(String[] args) { // Get current date and time Date currentDateTime = new Date(); System.out.println(currentDateTime); } }
Calendar class
The calendar class can also be used to get the current date and time. Using the class calendar you can get an instance of class Date. Following is the example that shows how you can use the Java Calendar class to print the current date and time.
import java.util.Calendar; import java.util.Date; public class DateTimeExample5 { public static void main(String[] args) { // Get instance of class Calendar Calendar calendar = Calendar.getInstance(); // Get instance of class Date Date currentDateTime = calendar.getTime(); System.out.println(currentDateTime); } }