Java Program to Get Current Date and Time

In this java program, we will learn about how to display current date and time in Java. You can get the current date and time in Java in various ways. Here we will discuss about getting current date and time using Date class, LocalDateTime class and Calendar class.
We will also learn about, how to display current date and time in the desired format using SimpleDateFormat class.


Java Program to Display Current Date and Time using Date Class

  • There is a class called Date class which can represent the current date and time in local timezone. We can import Date class from java.util package.

  • Here we will use SimpleDateFormat class to format the date time string in ""dd/MM/yy HH:mm:ss z"" format. However if you want the output in any other date format, just modify the pattern accordingly.

  • We can convert the current time to any specific timezone(UTC, GMT, PST etc) by setting timezone in date time formatter.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DisplayCurentDateTime {
  public static void main(String args[]) {
    // Getting current date and time using Date class
    Date date = new Date();
    // Print date time in default format
    System.out.println(date);
    // Define a formatter with date time format pattern
    DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss z");
    // Print date time in specific format
    System.out.println(df.format(date));
    // Setting specific time zone in formatter
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    // Print date time in specific timezone
    System.out.println(df.format(date));
  }
}
Output
Mon Nov 28 12:43:40 PST 2022
28/11/22 12:43:40 PST
28/11/22 20:43:40 GMT

Java Program to Display Current Date and Time using Calendar Class

  • In this program, we will use getInstance() method of Calendar class to get the Calander object representing the current date and time. We can import Calendar class from java.util package.

  • We will call the format() method of DateFormat and pass the Calendar.getTime() as a parameter to the method to display the date time in specific format.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class CurentDateTimeUsingCalendar {
  public static void main(String[] args) {
    // Getting current date and time using Calendar class
    Calendar calendarObj = Calendar.getInstance();
    // Print date time in default format
    System.out.println(calendarObj.getTime());
    
    // Define a formatter with date time format pattern
    DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss z");
    // Print date time in specific format
    System.out.println(df.format(calendarObj.getTime()));
    
    // Setting specific time zone in formatter
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    // Print date time in specific timezone
    System.out.println(df.format(calendarObj.getTime()));
  }
}
Output
Mon Nov 28 12:52:35 PST 2022
28/11/22 12:52:35 PST
28/11/22 20:52:35 GMT