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 use of the current date and time.
Class contents
This class contains basically two types of
objects.
First we create the object of the Date class. We create the Date class object using the new operator.
Date d=new Date();
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 returns
false.
- 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 the
specified date); if the values are the same then 0 is returned, it returnsa
negative value if the invoking object is earlier than the date and returns a
positive value if the invoking object is later.
- int compareTo(Object o)
Works automatically to compareTo(Date) if 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's
date 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
//import java.util.Date;
import java.util.*;
class DateEx
{
public static void main(String[] args)
{
// Initialize a Date object
Date dt = new Date();
// display date and time using toString()
System.out.println(dt.toString());
}
}
Output
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
import java.text.*;
import java.util.*;
public class DateEx2
{
public static void main(String args[])
{
Date d = new Date( );
SimpleDateFormat frmt=new SimpleDateFormat ("E
yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Current
Date: " + frmt.format(d));
}
}
Output
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
//import java.util.Date;
import java.util.*;
class DateEx3
{
public static void main(String args[])
{
// Initiate a Date object
Date d = new Date();
// display date and time using toString()
String s = String.format("Current
Date and Time is : %tc", d );
System.out.printf(s);
}
}
Output