Date Class in Java

This article explains how the Java Date class works.

Introduction

 
This article explains how the Java Date class works. This class is available in the "java.util.*" package, use it to print the current system time and date. This class is useful when we need to develop an application requiring the use of the current date and time.
 

Class contents

 
This class contains basically two types of objects.
  1. First, we create the object of the Date class. We create the Date class object using the new operator.
    Date d=new Date();
  2. The second type objects of the Date class is used to accept an argument. 
    Date(long miliseconds) d=new Date();
When we create a Date class object we can call any of the following support methods to play with the dates.
 
Public methods
 
The following are the public methods available in the Date class:
  • String toString( ) 
    Changes the given Date object into a string and return the result.
     
  • int hashCode( ) 
    Returns a hash code for the given object. 
     
  • void setTime(time) 
    Rearrange the date and time specified by time, that represents an elapsed time in milliseconds from midnight, January 1, 1970.
     
  • boolean after(Date date)  
    Returns true if the object's date is after the specified date, else returnsfalse.
      
  • boolean before(Date date)  
    Returns true if the object's date is before the specified date, else returns false. 
     
  • Object clone( )  
    It creates a clone of the date object. 
     
  • int compareTo(Date d)  
    Compares the value of two dates (in other words the object's date and thespecified date); if the values are the same then 0 is returned, it returnsanegative value if the invoking object is earlier than the date and returns apositive value if the invoking object is later.
      
  • int compareTo(Object o)  
    Works automatically to compareTo(Date) if an object(o) is of class Date.Otherwise, it throws a ClassCastException.
      
  • boolean equals(Object d)  
    After comparing values of both the two dates (in other words the object'sdate and the specified date) if they are the same it returns true,otherwise, it returns false.
      
  • long getTime( )  
    Shows the number of milliseconds used since January 1, 1970.

Date and time

 
By creating an object of the java.util.Date class.
 
DateEx.java
  1. import java.util.*;  
  2. class DateEx  
  3. {  
  4.  public static void main(String[] args)   
  5.  {  
  6.   Date dt = new Date();  
  7.   System.out.println(dt.toString());  
  8.  }  
  9. }  
Output
 
Fig-1.jpg
 

How to compare two dates in Java?

 
The following are three ways to compare two dates:
  • compareTo( ) method.
  • after( ), equals( ), and before( ) method.
  • getTime( ) method.

Formatting a date using SimpleDateFormat

 
It is a concrete class to format and parse dates. It allows us to start by choosing any user-defined pattern for date-time formatting.
 
For example:
 
DateEx2.java
  1. import java.text.*;  
  2. import java.util.*;  
  3. public class DateEx2   
  4. {  
  5.  public static void main(String args[])   
  6.  {  
  7.   Date d = new Date();  
  8.   SimpleDateFormat frmt = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");  
  9.   System.out.println("Current Date: " + frmt.format(d));  
  10.  }  
  11. }  
Output
 
Fig-2.jpg
 

Formatting date using printf

 
A date can be formated very easily using the printf method. We use a two-letter format, starting with t and ending in one of the letters (like M, d, H, s, S, etcetera).
 
For example:
 
DateEx3.java
  1. import java.util.*;  
  2. class DateEx3   
  3. {  
  4.  public static void main(String args[])   
  5.  {  
  6.   Date d = new Date();  
  7.   String s = String.format("Current Date and Time is : %tc", d);  
  8.   System.out.printf(s);  
  9.  }  
  10. }  
Output
 
Fig-3.jpg