Displaying the current date and time is a common requirement in Java applications. Java provides different classes and APIs to work with dates, times, and time zones.
- Date and time values are used for logging, transactions, timestamps, and scheduling.
- Older Java applications commonly use Date and SimpleDateFormat.
- Modern Java applications use the java.time package.
- Classes such as LocalDateTime, ZonedDateTime, and DateTimeFormatter provide flexible date and time handling.
Ways to Display the Current Date and Time
Java provides multiple ways to display the current date and time, ranging from traditional classes to the modern java.time API.
Method 1: Using the Date Class
The Date class belongs to the java.util package. It represents a specific instant in time and can be used to display the current date and time.
import java.util.Date;
public class CurrentDateTime {
public static void main(String[] args)
{
// Create a Date object
Date currentDate = new Date();
System.out.println("Current Date and Time: "+ currentDate);
}
}
Output
Current Date and Time: Thu Aug 27 03:24:49 UTC 2026
Explanation: The Date object stores the current date and time when it is created. The System.out.println() statement displays the date and time using the default format.
Note: The Date class is part of Java's legacy date and time API. For new programs, the modern java.time API is generally preferred.
Method 2: Using SimpleDateFormat to Format Date and Time
The SimpleDateFormat class can be used to display the current date and time in a specific format. It belongs to the java.text package.
Note: SimpleDateFormat is part of Java's legacy date and time API. For new applications, DateTimeFormatter from the java.time package is generally preferred.
import java.text.SimpleDateFormat;
import java.util.Date;
public class FormattedDateTime {
public static void main(String[] args)
{
// Create a SimpleDateFormat object
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
// Get the current date and time
Date date = new Date();
System.out.println("Current Date and Time: "+ formatter.format(date));
}
}
Output
Current Date and Time: 27/08/2026 03:27:25
Explanation: The pattern "dd/MM/yyyy HH:mm:ss" specifies the format of the date and time. The format() method converts the Date object into a formatted string.
Method 3: Using LocalDateTime
Java 8 introduced the java.time package, which provides a modern way to handle dates and times. LocalDateTime represents a date and time without any time-zone or offset information. LocalDateTime.now() obtains the current local date and time using the system's default time zone.
import java.time.LocalDateTime;
public class CurrentDateTime {
public static void main(String[] args)
{
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current Date and Time: " + currentDateTime);
}
}
Output
Current Date and Time: 2026-08-27T03:30:27.737538611
Explanation: LocalDateTime.now() obtains the current local date and time using the system's default time zone. LocalDateTime itself does not contain time-zone or offset information. The output follows the default ISO-8601-style format, where T separates the date and time.
Method 4: Using DateTimeFormatter with LocalDateTime
The DateTimeFormatter class can be combined with LocalDateTime to display the current date and time in a customized format.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatExample {
public static void main(String[] args)
{
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
String formattedDateTime = dateTime.format(formatter);
System.out.println("Formatted Date and Time: " + formattedDateTime);
}
}
Output
Formatted Date and Time: 27/08/2026 03:35:56
Explanation: DateTimeFormatter defines how the date and time should be displayed. The format() method applies the specified pattern to the LocalDateTime object.
Method 5: Using ZonedDateTime for a Specific Time Zone
LocalDateTime does not contain time-zone information. When an application needs to display the current time for a particular region, ZonedDateTime can be used.
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateTimeExample {
public static void main(String[] args)
{
// Specify the time zone
ZoneId zone = ZoneId.of("Asia/Kolkata");
ZonedDateTime dateTime = ZonedDateTime.now(zone);
System.out.println("Current Date and Time: " + dateTime);
}
}
Output
Current Date and Time: 2026-08-27T09:10:17.436489844+05:30[Asia/Kolkata]
Explanation: ZonedDateTime represents a date and time together with time-zone information. ZoneId.of("Asia/Kolkata") specifies the required time zone, and ZonedDateTime.now(zone) obtains the current date and time for that zone.
Method 6: Using the Clock Class
The Clock class is also part of the java.time package. It provides access to the current instant and can be useful when an application needs a clock source.
import java.time.Clock;
public class ClockExample {
public static void main(String[] args)
{
// Get the current time in UTC
Clock clock = Clock.systemUTC();
System.out.println("Current Time: " + clock.instant());
}
}
Output
Current Time: 2026-08-27T03:42:09.386358146Z
Explanation: Clock.systemUTC() creates a clock that uses UTC as its time zone. The instant() method returns the current moment as an Instant in UTC.
Method 7: Displaying Date and Time in 12-Hour Format
The DateTimeFormatter class can also be used to display the time in AM-PM format.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class AMPMDateTime {
public static void main(String[] args)
{
// Get the current date and time
LocalDateTime dateTime = LocalDateTime.now();
// Specify 12-hour format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm:ss a");
// Format the date and time
String formattedDateTime = dateTime.format(formatter);
System.out.println("Date and Time: " + formattedDateTime);
}
}
Output
Date and Time: 27/08/2026 03:44:30 AM
Explanation: The hh pattern represents the hour in 12-hour format, while a displays the AM or PM indicator. This makes the output easier to read in the standard 12-hour clock format.