Java Program For Date And Time

Chapter: Miscellaneous Last Updated: 24-03-2023 14:32:25 UTC


            /* ............... START ............... */
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeExample {
    public static void main(String[] args) {
        LocalDateTime now =;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        System.out.println("Current date and time: " + formattedDateTime);

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


Current date and time: 24-03-2023 11:25:30


  • The Java program provided above is a simple example of how to display the current date and time in a specific format.
  • We first import two classes, LocalDateTime and DateTimeFormatter, from the java.time package. These classes provide the functionality we need to work with dates and times in Java.
  • We then create a new class called DateTimeExample that contains a main method, which is the entry point for the program.
  • Inside the main method, we create a LocalDateTime object called now using the now() method. This object represents the current date and time.
  • We then create a DateTimeFormatter object called formatter using the ofPattern() method. This object specifies the format we want to use to display the date and time. In this case, the format is "dd-MM-yyyy HH:mm:ss", which means we want to display the day, month, year, hours, minutes, and seconds of the date and time.
  • Next, we use the format() method of the now object to format the date and time using the formatter object. This returns a String object representing the formatted date and time.
  • Finally, we print out the formatted date and time using the System.out.println() method. This displays the date and time on the console in the format we specified earlier.
  • So in summary, the program gets the current date and time, formats it in a specific way using a DateTimeFormatter object, and then prints out the formatted date and time using System.out.println().


#Java Program For Date And Time # Date Java example, #Java Date, #Time in Java, #Java get current date

Similar Programs Chapter Last Updated
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023
Java Program To Run Something Every X Minutes Miscellaneous 22-04-2023
Java Program To Reverse A String Miscellaneous 22-04-2023
Java Program To Implement Method Overloading Miscellaneous 16-04-2023
Java Parse JSON String To Object Miscellaneous 24-03-2023
Java Binary Search Tree Implementation Miscellaneous 21-03-2023
Java Program To Merge Two PDF Files Miscellaneous 18-03-2023
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Currency Formatter In Java | How To Format Currency In Java Miscellaneous 19-07-2021
Factory Design Pattern In Java Miscellaneous 11-05-2021
Data Types In Java Miscellaneous 09-06-2018