Java - Current Date and Time - GeeksforGeeks

13 May, 2022
In Java, there is a built-in class known as the Date class and we can import java.time package to work with date and time API. Here we are supposed to print current date and time. There can be multiple ways to print the current date and time.
Using Date ClassUsing get() method of Calendar classUsing calendar and formatter class to print the current dates in a specific format. 
Using Date Class
Using get() method of Calendar class
Using calendar and formatter class to print the current dates in a specific format. 
Method 1: Using Date Class
Example:
Java
// Java Program to Display Current Date and Time// Using Date class // Importing required classesimport java.util.*; // Classpublic class GFG {     // Main driver method    public static void main(String[] args)    {        // Creating object of date class        Date d1 = new Date();         // Printing the value stored in above object        System.out.println("Current date is " + d1);    }}
Current date is Thu Mar 31 01:14:28 UTC 2022
Method 2: Using get() method of Calendar class
getInstance() method is generally used to get the time, date or any required some belonging to Calendar year.
Tip: Whenever we require anyrhing belonging to Calendar, Calendar class is one of naive base for sure approach to deal with dae and time instances.
Example:
Java
// Java Program to Illustrate getinstance() Method// of Calendar Class // Importing required classesimport java.util.*; // Classclass GFG {     // Main driver method    public static void main(String[] args)    {        // Creating an object of Calendar class        Calendar c = Calendar.getInstance();         // Print corresponding instances by passing        // required some as in arguments        System.out.println("Day of week : "                           + c.get(Calendar.DAY_OF_WEEK));         System.out.println("Day of year : "                           + c.get(Calendar.DAY_OF_YEAR));         System.out.println("Week in Month : "                           + c.get(Calendar.WEEK_OF_MONTH));         System.out.println("Week in Year : "                           + c.get(Calendar.WEEK_OF_YEAR));         System.out.println(            "Day of Week in Month : "            + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));         System.out.println("Hour : "                           + c.get(Calendar.HOUR));         System.out.println("Minute : "                           + c.get(Calendar.MINUTE));         System.out.println("Second : "                           + c.get(Calendar.SECOND));         System.out.println("AM or PM : "                           + c.get(Calendar.AM_PM));         System.out.println("Hour (24-hour clock) : "                           + c.get(Calendar.HOUR_OF_DAY));    }}
Day of week : 7
Day of year : 83
Week in Month : 4
Week in Year : 12
Day of Week in Month : 4
Hour : 6
Minute : 22
Second : 48
AM or PM : 1
Hour (24-hour clock) : 18
 
Method 3: Using calendar and formatter class to print the current dates in a specific format.
Example:
Java
// Java Program to Demonstrate Working of SimpleDateFormat// Class // Importing required classesimport java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date; // Classclass GFG {     // Main driver method    public static void main(String args[])        throws ParseException    {         // Formatting as per given pattern in the argument        SimpleDateFormat ft            = new SimpleDateFormat("dd-MM-yyyy");         String str = ft.format(new Date());         // Printing the formatted date        System.out.println("Formatted Date : " + str);         // Parsing a custom string        str = "02/18/1995";        ft = new SimpleDateFormat("MM/dd/yyyy");        Date date = ft.parse(str);         // Printing date as per parsed string on console        System.out.println("Parsed Date : " + date);    }}
Formatted Date : 31-03-2022
Parsed Date : Sat Feb 18 00:00:00 UTC 1995
Akanksha_Rai
clintra
solankimayank
sagartomar9927
date-time-program
Java-Date-Time
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java