All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Get Current Date In Java

Last Updated : Mar 11, 2024

How To Get Current Date In Java

In this article we will show you the solution of how to get current date in java, to collect date you need two more package support util and text.

The util package helps to access date object through date constructor then text package imported for format the date to particular format as per your wish.

Now you can simply collect current date with the help of date object and converted as date string and displayed.

Step By Step Guide On How To Get Current Date In Java :-

In main class ‘CurDate_prgm’, we provide definition for public static main method. There you havr to define date object to retrieve today date then it returns stored on object variable 'd'.

The returned value has milliseconds date time format. To convert as date time string format we need to pass this date object 'd' to format() method.

Then you will get preferred format of date and stored on string variable at last we printed result of desired date.

//Current Date
package java_prgms;
import java.util.*;
import java.text.SimpleDateFormat;
class CurDate_prgm
 public static void main(String args[])
  Date d=new Date();
  SimpleDateFormat f=new SimpleDateFormat("dd/MM/yy");
  String s=f.format(d);
  System.out.println("Current Date Is "+s);
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. First displaying package will inserted by eclipse to support java codes, if you try codes in eclipse platform each time it will imported after successful creation of java project.
  3. The other two package is must, so remember to import them as foremost step.
  4. We defined main class ‘CurDate_prgm’, ensure whether you are given name for class and saved program name same or not. If same you do not get confuse at any case during execution otherwise during execution you have to carefully give excution commend with appropriate name.
  5. Within main class you might specify main method there we defined date object 'd' with 'Date()' constructor. This successful date object creation holds current date from local your local server.
  6. To get proper format of date we defined SimpleDateFormat object 'f' like date object. To change the date format we appending object 'f' with format() method. There you need to pass date object 'd' then we printed retrieved date with string 'Current Date Is'.
  7. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  8. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to get current date in java. To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java CurDate_prgm‘ line for gets the output.

When you run the program by simply clicks on run button on eclipse will display output ‘Current Date Is 23/12/2022'. This date will vary based on your executing date.

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪