Table of contents
1.
Introduction
2.
Date Class
2.1.
Implementation
2.2.
Java
3.
Constructor
3.1.
Example
3.1.1.
Implementation
3.2.
Java
4.
Methods of java. util.Date
5.
Method
5.1.
Example
5.1.1.
Implementation
5.2.
Java
6.
Frequently Asked Questions
6.1.
What is a package in Java?
6.2.
What is the java.util package?
6.3.
How to write date in Java?
6.4.
Which method of the date class is used to compare two dates?
7.
Conclusion
Last Updated: Sep 22, 2025
Easy

Date Class in Java

Author Nagendra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is one of the most popular programming languages. Learning Java reinforces essential computer science principles while also providing access to a wide range of professional options. Packages are used in Java to arrange classes and interfaces.
The Date class is part of the java.util package that encapsulates the current date and time. It has various constructors and methods. 

“java date class

This blog describes the Date Class in Java, including its constructors, methods, and examples.

Read More About, Basics of Java

Without further ado, let's get started.

Date Class

The Date class represents a precise moment in time, down to the millisecond. Serializable, Cloneable and Comparable interfaces are implemented by the Date class in java.util package. It has constructors and methods for working with dates and times in Java. 

Take a look at the code snippet below to print the current date.

Implementation

  • Java

Java

import java.util.Date;
public class Ninja
{
  public static void main(String args[])
  {
     Date date = new Date(); //Creating object of Date Class
     System.out.println(date); //prints the current Date
  }
}
You can also try this code with Online Java Compiler
Run Code

Output

Thu Jun 09 07:54:19 GMT 2022
You can also try this code with Online Java Compiler
Run Code

Explanation

The Date() sets the current date and time.

Let's continue by learning different constructors of the Date class in Java.

Constructor

The constructors of the date class are listed below.

Let's have a look at an example to see how to use them.

Example

The following code snippet explains the implementation of the constructors of Date Class.

Implementation

  • Java

Java

import java.util.*;
public class Ninja
{
     public static void main(String[] args)
  {
     Date d1 = new Date(); //Use of Date()
     System.out.println("The Current date is " + d1);
     Date d2 = new Date("2018 Jun 28"); //Date(String s)
     System.out.println("The Date represented is "+ d2 );
  }
}
You can also try this code with Online Java Compiler
Run Code

Output

The Current date is Thu Jun 09 13:42:12 IST 2022
The Date represented is Thu Jun 28 00:00:00 IST 2018
You can also try this code with Online Java Compiler
Run Code

Explanation

The first constructor initialises the current date. The second constructor converts the date from String format to Date format.

Let's go on to understand the various methods of the Java Date class. Try it on java online compiler.

Methods of java. util.Date

Certainly! Here is a table that explains the methods of `java.util.Date`:

MethodDescriptionReturns
boolean after(Date when)Tests if this date is after the specified date.boolean
`boolean before(Date when)Tests if this date is before the specified date. boolean
Object clone()Creates a copy of this date objectObject
int compareTo(Date anotherDate)Compares two dates for ordering.int
boolean equals(Object obj)Compares this date to the specified object. boolean
long getTime()Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date objectlong
int hashCode() Returns a hash code value for this date.int
String toString()Converts this Date object to a String of the form: "dow mon dd hh:mm:ss zzz yyyyString
int getDate()Returns the day of the month represented by this Date object. (Deprecated)int
int getDay()Returns the day of the week represented by this Date object. (Deprecated)int
int getMonth()Returns the month represented by this Date object. (Deprecated)int
int getYear()Returns the year represented by this Date object, minus 1900. (Deprecated)int

Method

The methods of the date class are listed below.

Let's have a look at an example to see how to use them.

Example

The following code snippet explains the uses of the methods of Date Class.

Implementation

  • Java

Java

import java.util.*;
public class Ninja
{
     public static void main(String[] args)
  {
     Date d1 = new Date(2001, 05 , 28);
     Date d2 = new Date(); // It initialises the Current date
     Date d3 = new Date(2012, 1, 3);
     boolean a = d3.after(d1);
     System.out.println("Date 3-1-2012 comes after " +"date 28-05-2001: " + a);
     int c = d1.compareTo(d2);
     System.out.println(c); // 1
     System.out.println("Milliseconds from Jan 1 "+"1970 to date 28-05-2001 is " + d1.getTime());
     System.out.println("Before Changing The Date"+d2);
     d2.setTime(507587433443L); //The New date in Milliseconds is passed as argument
     System.out.println("The new Date is "+d2);
  }
}
You can also try this code with Online Java Compiler
Run Code

Output

Date 3-1-2012 comes after date 28-05-2001: true
1
Milliseconds from Jan 1 1970 to date 28-05-2001 is 60951810600000
Before Changing The DateThu Jun 09 13:59:30 IST 2022
The new Date is Sat Feb 01 02:00:33 IST 1986
You can also try this code with Online Java Compiler
Run Code

Explanation

The first statement prints true because date 3-1-2012 comes after date 28-05-2001.
The second statement prints 1 because the invoking date is greater than the former one.
The third statement prints the value in milliseconds from Jan 1, 1970.
The fourth and fifth statements print the dates before and after changing, respectively.

Frequently Asked Questions

What is a package in Java?

A Java package is a collection of similar classes, interfaces, and sub-packages.

What is the java.util package?

The java.util package contains a collections framework, legacy collection classes, date and time facilities, event model, internationalisation, and other utility classes are all included (a string tokenizer, a random-number generator, and a bit array).

How to write date in Java?

To write the current date in Java, you can use the LocalDate class from the java.time package:

LocalDate currentDate = LocalDate.now(); System.out.println(currentDate);

This prints the current date in the format YYYY-MM-DD.

Which method of the date class is used to compare two dates?

The compareTo() method is used to compare two date values.

Conclusion

In this article, we have extensively discussed the Date Class in Java along with the details of its constructors, methods, and examples.
We hope that this blog has helped you enhance your knowledge regarding the Date Class in Java, and if you would like to learn more about Data Structures and Algorithms, you can enroll in our course on DSA in Java.

Recommended Readings:

Multiple Inheritance in Java.

Live masterclass