The Wayback Machine - https://web.archive.org/web/20240703171401/https://www.geeksforgeeks.org/java-current-date-time/
Open In App

Java – Current Date and Time

Last Updated : 08 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In software development, we often work with dates and times. To accurately capture the current date and time is fundamental for various applications, such as scheduling tasks, tracking events, etc.

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 the 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 the Calendar class
  3. Using calendar and formatter class to print the current dates in a specific format. 
  4. Using java.time.LocalDate
  5. Using java.time.LocalTime
  6. Using java.time.LocalDateTime
  7. Using java.time.Clock
  8. Using java.sql.Date

Using Date Class

Using Date Class in this method we will explore the date and time module provided by java.util.

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 Nov 30 07:45:38 UTC 2023



Using Calendar Instance

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.

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 : 5
Day of year : 334
Week in Month : 5
Week in Year : 48
Day of Week in Month : 5
Hour : 7
Minute : 45
Second : 39
AM or PM : 0
Hour (24-hour clock) : 7



Using SimpleDateFormat

Using calendar and formatter class to print the current dates in a specific format.

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 : 04-01-2024
Parsed Date : Sat Feb 18 00:00:00 UTC 1995

Using LocalDate, LocalTime, LocalDateTime

In this method we will discuss various date and time methods provided by java.time which

Java




// java program to use Date and time
// module in java.time package
  
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
  
// Driver class
public class DateTimeExample {
  
      //Main method
    public static void main(String[] args){
        // Current date
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current date: " + currentDate);
  
        // Current time
        LocalTime currentTime = LocalTime.now();
        System.out.println("Current time: " + currentTime);
  
        // Current date and time
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("Current date and time: "
                           + currentDateTime);
    }
}


Output

Current date: 2024-01-04
Current time: 11:59:03.285876
Current date and time: 2024-01-04T11:59:03.286975

Using System Clock

This method we will discuss the use of clock method to fetch date and time provided by java.time package.

Java




//Java program to fetch 
//current system date and time
// using Clock
  
import java.time.Clock;
  
//Driver class
public class ClockExample {
  
      //Main method
    public static void main(String[] args) {
        // Get the default system clock
        Clock systemClock = Clock.systemDefaultZone();
  
        // Get the current instant using the clock
        System.out.println("Current instant: " + systemClock.instant());
    }
}


Output

Current instant: 2024-01-04T11:58:52.703945Z


Similar Reads

How to Get Current Time and Date in Android?
Many times in android applications we have to capture the current date and time within our android application so that we can update our date according to that. In this article, we will take a look at How to get the current Time and Date in our android application. Note: This Android article covered in both Java and Kotlin languages. Step by Step I
3 min read
C++ Program to Print Current Day, Date and Time
In order to facilitate finding the current local day, date, and time, C++ has defined several functions in the header file, so functions that will help us in achieving our objective of finding the local day, date, and time are: time(): It is used to find the current calendar time.Its return type is time_t, which is an arithmetic data type capable o
2 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
New Date-Time API in Java 8
New date-time API is introduced in Java 8 to overcome the following drawbacks of old date-time API : Not thread safe : Unlike old java.util.Date which is not thread safe the new date-time API is immutable and doesn't have setter methods.Less operations : In old API there are only few date operations but the new API provides us with many date operat
6 min read
LocalDateTime of(date, time) method in Java with Examples
The of(LocalDate date, LocalTime time) method of LocalDateTime class in Java is used to obtain an instance of LocalDateTime using the two input parameters, date and time. Initially, two separate instances are created. One is of LocalDate type and the other one is of LocalTime type. Then these are merged to create a LocalDateTime. Syntax: public sta
2 min read
Find the date after next half year from a given date
Given a positive integer D and a string M representing the day and the month of a leap year, the task is to find the date after the next half year. Examples: Input: D = 15, M = "January"Output: 16 JulyExplanation: The date from the 15th of January to the next half year is 16th of July. Input: D = 10, M = "October"Output: 10 April Approach: Since a
7 min read
Date after adding given number of days to the given date
Given a date and a positive integer x. The task is to find the date after adding x days to the given date Examples: Input : d1 = 14, m1 = 5, y1 = 2017, x = 10Output : d2 = 24, m2 = 5, y2 = 2017 Input : d1 = 14, m1 = 3, y1 = 2015, x = 366Output : d2 = 14, m2 = 3, y2 = 2016 Method 1: 1) Let given date be d1, m1 and y1. Find offset (number of days spe
12 min read
Difference between Compile-time and Run-time Polymorphism in Java
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. In this article, we will see the difference between two types of polymorphisms, compile time and run time. Compile Time Polymorphism: Whenever an object is bound with its functionality at the
4 min read
Naming a thread and fetching name of current thread in Java
Thread can be referred to as a lightweight process. Thread uses fewer resources to create and exist in the process; thread shares process resources. The main thread of Java is the thread that is started when the program starts. now let us discuss the eccentric concept of with what ways we can name a thread. Methods: There are two ways by which we c
5 min read
Get name of current method being executed in Java
Getting name of currently executing method is useful for handling exceptions and debugging purposes. Below are different methods to get currently executing method : Using Throwable Stack Trace : Using Throwable Class : In Java, Throwable class is the superclass of all exceptions and errors in java.lang package. Java Throwable class provides several
4 min read
Practice Tags :
three90RightbarBannerImg