All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Get Current Date

Last Updated : Mar 11, 2024

Java Get Current Date

In this article we will show you the solution of java get current date, when the LocalDateTime.now() method is called, the LocalDateTime instance is returned. The current date and time are displayed when we print the instance of the LocalDateTime class.

The DateTimeFormatter class is included in JDK 1.8 and can be used to format the current date. Java programming language will be used in this article to get current date and time.

The Java language offers several ways to retrieve current dates and times. Let's explore them in depth. We will now discuss the idea of how to get the current date in java with an example.

Step By Step Guide On Java Get Current Date :-

// Java Program to illustrate
// getting the current date
// Importing the LocalDate class
// to get the current date
importjavaa.t ime.LocalDate;
// Public class
public class MyClass {
    public static void main(String[] args) {
            // creating the instance of the
            // LocalDate class using the now() method
            LocalDate date = LocalDate.now();
            // Fetching yesterday's date
            LocalDate yesterday = date.minusDays(1);
            // Fetching tomotomorrow'se
            LocalDate tomorrow = yesterday.plus days(2);
            // Printing today's date
            System. out.println("Today date: "+date);
            // Printing yesterday's date
            System. out.println("Yesterday date: "+yesterday);
            // Printing tomorrow's date
            System. out.println("Tomorrow date: "+tomorrow);
    }
}
  1. The current date can be obtained by importing the LocalDate class.
  2. Creating a public class named MyClass is the first step.
  3. Describe how the program should be executed in the main method.
  4. The current date will be fetched using a local variable named date that extends the LocalDate class from the now() method.
  5. You can fetch yesterday's date using the minusDays() method by creating another instance variable named yesterday.
  6. The future date can be retrieved by creating another instance variable named tomorrow using the plusDays() method.
  7. With the println() method, you can display today's date.
  8. In this example, we will print yesterday's date using the println() method.
  9. The println() method can be used to print tomorrow's date.
  10. Execute the program until it has finished.

Conclusion :-

As a result, we have successfully learned how to get the current date in java with an example.

The objective of this article was to teach how to obtain current dates and times in Java.

I would like to summarize the points that we discussed throughout the article.

In Java, there are several different methods for getting current dates and times.

Using the DateTimeFormatter class included in JDK 1.8, we can change the date and time format of our date and time.

The SimpleDateFormat class can be used to retrieve the current date and time in whichever format you wish in Java.

I hope this article on java get current date helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪