Write A Program To Display Current Date And Time In Java

Chapter: Date and Time Last Updated: 22-06-2023 13:57:57 UTC

Program:

            /* ............... START ............... */
                

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class CurrentDateTimeExample {
    public static void main(String[] args) {
        // Get the current date and time
        LocalDateTime currentDateTime = LocalDateTime.now();

        // Define the date and time format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // Format the current date and time using the defined format
        String formattedDateTime = currentDateTime.format(formatter);

        // Display the formatted current date and time
        System.out.println("Current Date and Time: " + formattedDateTime);
    }
}

                /* ............... END ............... */
        

Output

Current Date and Time: 2023-06-22 12:34:56

Notes:

  • First, we import the necessary classes: LocalDateTime from the java.time package and DateTimeFormatter from the java.time.format package. These classes provide convenient methods to work with dates and times in Java.
  • Next, we define a class called CurrentDateTimeExample. This class will contain our main method where the program execution starts.
  • Inside the main method, we use the LocalDateTime.now() method to get the current date and time. This method retrieves the current date and time information from the system clock.
  • We create a DateTimeFormatter object named formatter by calling the DateTimeFormatter.ofPattern() method. This method allows us to define a specific pattern for formatting the date and time.
  • We specify the pattern "yyyy-MM-dd HH:mm:ss" in the ofPattern() method. This pattern represents the desired format for the date and time, where yyyy represents the year, MM represents the month, dd represents the day, HH represents the hour in 24-hour format, mm represents the minute, and ss represents the second.
  • Now, we format the current date and time using the format() method of the LocalDateTime class. We pass the formatter object as an argument to the format() method to apply the desired formatting pattern.
  • The result of the formatting operation is stored in a String variable called formattedDateTime.
  • Finally, we use System.out.println() to display the formatted date and time on the console, along with some additional text.
  • When you run this program, it will retrieve the current date and time, format it according to the specified pattern, and print the formatted date and time on the console.
Similar Programs Chapter Last Updated
Java Program to Calculate Time Difference In Hours Date and Time 20-09-2023
Java Code To Calculate Time Difference In Milliseconds Date and Time 20-09-2023
Java Program To Calculate Time Difference In Seconds Date and Time 20-09-2023
How to calculate time difference between two time zones in Java Date and Time 20-09-2023
Java Date And Time API To Create A Custom Calendar Date and Time 20-09-2023
How To Format Date And Time In Java Date and Time 16-09-2023
How Do You Get The First Day Of The Week In Java Date and Time 15-09-2023
Java Code To Change Date Format Date and Time 15-08-2023
Java Program To Find Day Of The Week For A Given Date Date and Time 10-08-2023
Java code to check if date is weekend Date and Time 29-06-2023
Java program to find number of days between two dates Date and Time 29-06-2023
Java Program To Get The Current Time In Different Time Zones Date and Time 22-06-2023
How To Add Working Days To A Date In Java Date and Time 22-06-2023
Java Program To Add A Specified Number Of Days To A Given Date Date and Time 22-06-2023
Write A Program To Check Leap Year In Java Date and Time 22-06-2023
Java Date Format AM PM Date and Time 14-05-2023
Java Add Months To Date Date and Time 14-05-2023
Add Days To Date In Java example Date and Time 14-05-2023
Java Program To Calculate Days Between Two Dates Date and Time 16-04-2023
Java Program To Add Days To Date Date and Time 16-04-2023
How To Find Difference Between Two Dates In Java Date and Time 01-04-2023
Add Days To Date Java Example Date and Time 08-08-2021
Time Difference Between Two Timestamps In Java Date and Time 22-09-2018

1