Java Current Date and Time
We can quickly obtain the current date and time in Java by utilizing SimpleDateFormat and the Date/Calendar class. We'll review how to use the Date and Calendar class to retrieve the current date and time and SimpleDateFormat to get it in your chosen format.
Read More About, Basics of Java
Different Ways To Get Current Date And Time
Here are different ways to get the current date and time in Java,
1. Using LocalDateTime
- Description: Get the current date and time without time zone information.
- Code:
import java.time.LocalDateTime; LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println(currentDateTime);
2. Using LocalDate
- Description: Get the current date without time information.
- Code:
import java.time.LocalDate;
LocalDate currentDate = LocalDate.now();
System.out.println(currentDate);
3. Using LocalTime
- Description: Get the current time without date information.
- Code:
import java.time.LocalTime;
LocalTime currentTime = LocalTime.now();
System.out.println(currentTime);
4. Using ZonedDateTime
- Description: Get the current date and time with time zone information.
- Code:
import java.time.ZonedDateTime;
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
5. Using Instant
- Description: Get the current timestamp as an instance of Instant (UTC).
- Code:
import java.time.Instant;
Instant currentInstant = Instant.now();
System.out.println(currentInstant);
6. Using Calendar (Legacy)
- Description: Get the current date and time using the Calendar class.
- Code:
import java.util.Calendar;
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime());
The following methods are used to obtain the current date in the Java program when the need arises:
Using Date Class
When establishing an instance of the SimpleDateFormat, the user specifies the needed pattern. The parameter is the date object passed to the format() function of the DateFormat class.
We will need to import "java.text.SimpleDateFormat" and "java.util.Date".
Example
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
DateFormat dfrm = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Date dt = new Date();
System.out.println(dfrm.format(dt));
}
}

You can also try this code with Online Java Compiler
Run CodeOutput:
10/07/22 16:27:51
Using Calendar Class
The getInstance function is used to construct an object of the Calendar class (). The calendar is the name of the format method. The method accepts getTime() as an argument.
Example
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
import java.util.Calendar;
public class Main
{
public static void main(String[] args) {
DateFormat dfrm = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Calendar dt = Calendar.getInstance();
System.out.println(dfrm.format(dt.getTime()));
}
}

You can also try this code with Online Java Compiler
Run CodeOutput:
10/07/22 16:27:17
Using SimpleDateFormat
The SimpleDateFormat class in Java is a part of the java.text package and is utilized for formatting and parsing dates in a locale-sensitive manner. This class allows developers to define a pattern that represents how the date and time should be formatted as a string.
Example
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
// Create a SimpleDateFormat instance with a specific pattern
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
// Create a Date object representing the current date and time
Date currentDate = new Date();
// Format the current date into a string using the formatter
String formattedDate = formatter.format(currentDate);
// Output the formatted date
System.out.println("Current Date and Time: " + formattedDate);
}
}

You can also try this code with Online Java Compiler
Run CodeOutput
Current Date and Time: 28-10-2024 14:30:15
Date/Time API
- LocalDate
- LocalTime
- LocalDateTime
- ZonedDateTime
In Java 8, a new API was provided. To replace Java.util.Date and java.util.calendar was the only reason for doing this. The following classes make it up:
LocalDateTime
The LocalDateTime class instance is returned by this method, which also prints the current date and time.
System.out.println(java.time.LocalDateTime.now());
LocalDate
Simply put, this method returns the current Date.
LocalDate date = LocalDate.now();
LocalTime
The current time is all that this method returns, and it supports formatting.
LocalTime time = LocalTime.now();
ZonedDateTime
This approach can be utilized in the following ways depending on the time zone.
ZonedDateTime dateTime = ZonedDateTime.now();
Code Implementation
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class DateTimeExample {
public static void main(String[] args) {
// 1. Using LocalDate
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date: " + currentDate);
// 2. Using LocalTime
LocalTime currentTime = LocalTime.now();
System.out.println("Current Time: " + currentTime);
// 3. Using LocalDateTime
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current Date and Time: " + currentDateTime);
// 4. Using ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println("Current Date and Time in New York: " + zonedDateTime);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Current Date: 2024-10-28
Current Time: 14:30:15.123456789
Current Date and Time: 2024-10-29T14:30:15.123456789
Current Date and Time in New York: 2024-10-29T14:30:15.123456789-04:00[America/New_York]
Using System Clock in Java
In Java, you can use the system clock to retrieve the current date and time in various formats. The java.time.Clock class provides a way to obtain the current time from the system clock. Below is an explanation with code implementation using the system clock and the expected output.
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class SystemClockExample {
public static void main(String[] args) {
// Create a Clock instance that uses the system clock
Clock clock = Clock.systemDefaultZone();
// 1. Get the current Instant
Instant currentInstant = clock.instant();
System.out.println("Current Instant: " + currentInstant);
// 2. Get the current LocalDateTime using the system clock
LocalDateTime currentDateTime = LocalDateTime.now(clock);
System.out.println("Current LocalDateTime: " + currentDateTime);
// 3. Get the current date and time in a specific time zone
ZoneId zoneId = ZoneId.of("America/New_York");
LocalDateTime currentDateTimeInZone = LocalDateTime.now(clock.withZone(zoneId));
System.out.println("Current LocalDateTime in New York: " + currentDateTimeInZone);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Current Instant: 2024-10-28T18:45:15.123456Z
Current LocalDateTime: 2024-10-29T14:45:15.123456
Current LocalDateTime in New York: 2024-10-29T14:45:15.123456
Examples of Current Date and time in Java
Code 1
In this code, we will be getting the current date and time.
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CurrentDateAndTime {
public static void main(String[] args) {
//Using the Date class, obtain the current date and time
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Date dateobj = new Date();
System.out.println(df.format(dateobj));
/*getting current date time using calendar class
* An Alternative of above*/
Calendar calobj = Calendar.getInstance();
System.out.println(df.format(calobj.getTime()));
}
}

You can also try this code with Online Java Compiler
Run CodeOutput
06/07/22 20:35:27
06/07/22 20:35:27

You can also try this code with Online Java Compiler
Run CodeIn this example, we successfully get the current date and time.
Code 2
In this example, we shall get the current date and time in another time zone.
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class CurrentDateTimeinDifferentZone {
public static void main(String[] args) {
//"hh" in pattern is for 12 hour time format and "aa" is for AM/PM
SimpleDateFormat dateTimeInGMT = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss aa");
//Setting the time zone
dateTimeInGMT.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateTimeInGMT.format(new Date()));
}
}

You can also try this code with Online Java Compiler
Run CodeOutput
2022-Jul-06 03:11:15 pm

You can also try this code with Online Java Compiler
Run CodeIn this example, we have successfully gotten the current date and time of different time zone.
Practice it on online java compiler for better understanding.
Check out this article - Upcasting and Downcasting in Java
Real-World Use Cases of Date and Time in Java
Java’s powerful date and time APIs solve practical problems developers encounter daily. Here are three common scenarios where they’re actively used in real-world applications.
1. Logging and Auditing in Banking Systems
In fintech or banking apps, every transaction must be logged with an exact timestamp for compliance and security audits. Java’s Instant or ZonedDateTime is used to capture events in real time.
Instant timestamp = Instant.now();
This ensures that every fund transfer, login, or change request is traceable and time-bound.
2. Scheduling Reminders in Health Apps
Java applications that power health services or medication reminders often need to schedule future tasks. Classes like Timer, ScheduledExecutorService, and ZonedDateTime help trigger actions at the correct local time, even across time zones.
ZonedDateTime scheduleTime = ZonedDateTime.now().plusHours(6);
Used in mobile or web apps that rely on punctual notifications.
3. Timestamps in E-commerce Orders
E-commerce platforms like Amazon or Flipkart use LocalDateTime to show order placement times, estimated delivery, and status updates. These timestamps improve user trust and provide a detailed order trail.
LocalDateTime orderTime = LocalDateTime.now();
A user might see: “Order placed on July 4, 2025 at 2:30 PM.”
Frequently Asked Questions
What is the date and time in Java?
The current date and time are contained in the Date class, which Java offers and is included in the Java.util package. The following table displays the two constructors that the Date class provides. The current date and time are used to initialize the object in this function Object().
Can Java Handle Time Zones When Getting the Current Date and Time?
Yes, Java can handle time zones using the java.time package. Classes like ZonedDateTime and ZoneId allow developers to work with the current date and time in specific time zones easily.
What Is the Easiest Way to Get the Current Date and Time in Java?
The easiest way to get the current date and time in Java is by using the LocalDateTime.now() method from the java.time package, which retrieves the date and time from the system clock in the default time zone.
What Are the Commonly Used Classes to Work with Dates and Times in Java?
Commonly used classes to work with dates and times in Java include LocalDate, LocalTime, LocalDateTime, ZonedDateTime, and Instant. These classes provide various functionalities to manage dates and times effectively.
Conclusion
In this article, we learned various methods to retrieve the current date and time in Java. We looked at the different formatting options for dates and discussed several Date/Time APIs available in Java, providing insights into how to effectively manage date and time representations in applications.