Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava – Current Date and Time

Java – Current Date and Time

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.

Different Ways To Get Current Date And Time

  1. Using Date Class
  2. Using get() method of Calendar class
  3. 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 classes
import java.util.*;
 
// Class
public 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);
    }
}


Output

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 anything belonging to Calendar, Calendar class is one of naive base for sure approach to deal with date and time instances.

Example:

Java




// Java Program to Illustrate getinstance() Method
// of Calendar Class
 
// Importing required classes
import java.util.*;
 
// Class
class 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));
    }
}


Output: 

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 classes
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
// Class
class 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);
    }
}


Output

Formatted Date : 31-03-2022
Parsed Date : Sat Feb 18 00:00:00 UTC 1995
RELATED ARTICLES

Most Popular

Recent Comments