Algorithm


  1. Import Necessary Packages:

    • Import the required packages to access date and time-related classes.
  2. Create a Date Object:

    • Create an object of the java.util.Date class to represent the current date and time.
  3. Create a Calendar Object:

    • Create an object of the java.util.Calendar class and set its time to the current date and time.
  4. Get Current Date and Time:

    • Use the getTime() method of the Date object to get the current date and time.
    • Use the get methods of the Calendar object to extract individual components like year, month, day, hour, minute, and second.
  5. Print or Use the Current Date/Time:

    • Print or use the extracted date and time components as needed in your program.

 

Code Examples

#1 Code Example- Get Current date and time in default format

Code - Java Programming

import java.time.LocalDateTime;

public class CurrentDateTime {

    public static void main(String[] args) {
        LocalDateTime current = LocalDateTime.now();

        System.out.println("Current Date and Time is: " + current);
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Current Date and Time is: 2023-12-07T5:25:44.973

#2 Code Example- Get Current date and time with pattern

Code - Java Programming

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

public class CurrentDateTime {

    public static void main(String[] args) {
        LocalDateTime current = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        String formatted = current.format(formatter);

        System.out.println("Current Date and Time is: " + formatted);
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Current Date and Time is: 2023-12-07 5:29:57.401

#3 Code Example- Get Current Date time using predefined constants

Code - Java Programming

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

public class CurrentDateTime {

    public static void main(String[] args) {
        LocalDateTime current = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
        String formatted = current.format(formatter);

        System.out.println("Current Date is: " + formatted);
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Current Date is: 20170802

#4 Code Example- Get Current Date time in localized style

Code - Java Programming

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

public class CurrentDateTime {

    public static void main(String[] args) {
        LocalDateTime current = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        String formatted = current.format(formatter);

        System.out.println("Current Date is: " + formatted);
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Current Date is: Dec 2, 2023 11:44:19 AM
Advertisements

Demonstration


Java Programing Example to Get Current Date/TIme-DevsEnv