Java Program to Get Current Date and Time

Java Program to Get Current Date and Time

What is  an Execution Time In Java ?

Execution time, also known as run time, refers to the amount of time it takes for a Java program or a specific method within a program to complete its execution. This can be useful for measuring the performance of a program or specific parts of a program, and can be used to identify bottlenecks or optimize performance.

Steps  to get Current Date and Time : 

  • Import the java.time package by adding the following line at the top of your code: import java.time.*;
  • Use the LocalDate class to get the current date. This class has a now() method that returns the current date in the system’s time zone. For example:
LocalDate today = LocalDate.now();
  • Use the LocalDateTime class to get the current date and time. This class also has a now() method that returns the current date and time in the system’s time zone. For example:
LocalDateTime now = LocalDateTime.now();
  • Print the result to the console using the System.out.println() method. For example:
System.out.println("Today's date: " + today);
System.out.println("Current time: " + now);

Here is the pseudo code for getting the current date and time in Java:

import java.time.*;

// Get current date
LocalDate today = LocalDate.now();

// Get current time
LocalTime now = LocalTime.now();

// Get current date and time
LocalDateTime now = LocalDateTime.now();

// Print results
print("Today's date: " + today);
print("Current time: " + now);
print("Current date and time: " + now);

Example :

Run
import java.time.*;

public class Main {
    public static void main(String[] args) {
        // Get current date
        LocalDate today = LocalDate.now();
        // Get current time
        LocalTime now = LocalTime.now();
        // Get current date and time
        LocalDateTime nowDateTime = LocalDateTime.now();

        // Print results
        System.out.println("Today's date: " + today);
        System.out.println("Current time: " + now);
        System.out.println("Current date and time: " + nowDateTime);
    }
}

Output :

Today's date: 2023-01-27
Current time: 10:12:03.083144
Current date and time: 2023-01-27T10:12:03.083437
Note : Current time may veries depending on which time you are using it.

Explanation : 

In this example, The LocalDate, LocalTime, and LocalDateTime classes are imported from the java.time package at the top of the code.

In the main method, the now() method is called on each of the classes to get the current date, time, and date and time, respectively. The results are then stored in the variables today, now, and now.

Finally, the results are printed to the console using the System.out.println() method.

Example :

Run
import java.time.*;

public class Main {
    public static void main(String[] args) {
        // Get current date and time with timezone
        ZonedDateTime nowZoned = ZonedDateTime.now();
        // Print results
        System.out.println("Current date and time with timezone: " + nowZoned);
    }
}

Output :

Current date and time with timezone: 2023-01-27T10:14:42.792921Z[GMT]

Explanation :

This will give you the current date time and timezone information, The output will be something like this “Current date and time with timezone: 2020-01-01T12:00:00+05:30[Asia/Kolkata]”

Note : The date, time, and date and time may vary depending on the system’s time zone and current date and time.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription