Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Date Class
3.
Constructor
3.1.
Example
4.
Method
4.1.
Example
5.
Frequently Asked Questions
5.1.
What is a package in Java?
5.2.
What is the java.util package?
5.3.
Which method of the date class is used to compare two dates?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Date Class in Java

Author Nagendra
0 upvote

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. 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.

Code

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
   }
}

Output

Thu Jun 09 07:54:19 GMT 2022

Explanation

The Date() sets the current date and time.

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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.

Code

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 );
   }
}

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

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.

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.

Code

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);
   }
}

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

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.

You can also read about the Multiple Inheritance in Java.

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).

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.

Do upvote our blog to help other ninjas grow. 

Happy Coding!!

Live masterclass