Java Date Class Example

The class Date is nothing but the representation of the specific instant in Time. It is given in the millisecond precision. For calculation purpose, all the operating system assumes that 1 day= 24*60*60=86400 seconds. However, in Coordinated Universal Time (UTC), there is an extra second called “leap second” about once every year or two year which is added either on December 31 or June 30.

also read:

In Class Date two additional functions were included prior to the JDK 1.1. First, the interpretations of dates were done as year, month, day, hour, minute, and second values. Second, formatting and parsing of the date strings. But, these functions are now taken out of Class Date and are included in the Class Calendar which is a part of JDK 1.1.Date standard for some computer is defined in terms of Greenwich Mean Time (GMT), which is equivalent to the Universal Time (UT).

Date Class Declaration

 public class date extends Object implements Serializable, Cloneable,
Comparable<Date>

The above declaration defines an object of class date which allows the serialization and colonizing of the objects. Serialization is an object can be converted to the series of bytes, which contains the information of the object’s data like its type of data stored in that object. Once serialized data is been written to the file it can be deserialized to the object whenever is required. Colonizing allows making the duplicate copy of the object.

Date Class Constructors

ConstructorDescription
Date ()It assigns a Date object in such a way that it represents the instant at which it is was allocated.
Date (long date)It assigns a Date object in such a way that it represents the standard base time known as “the epoch”, namely January 1, 1970, 00:00:00 GMT.

Date Class Methods

MethodDescription
boolean after(Date when)It returns boolean true value, if the date is after the specified date.
boolean before(Date when)It returns boolean true value, if the date is before the specified date.
Object clone()It creates the duplicate copy of the object.
int compareTo (Date anotherDate)To make ordering of dates, it compares the two dates.
boolean equals(Object obj)Compares the two date to check equality . If equal it returns zero value.
long getTime ()For the given date object, it returns the number of milliseconds passed since January 1, 1970, 00:00:00 GMT.
int hashcode ()It generates the hash code for the date object.
void setTime (long Time)It sets the time for the date object after January 1, 1970, 00:00:00 GMT.
String toStringIt gives the string form for the date Object.

Date Class Example

import java.util.*;
public class Example_date {
	public static void main(String[] args) {

	      Date date1 = new Date (90, 8, 3);
	      Date date2 = new Date (90, 11, 18);

	      boolean before = date2.before(date1);
	      System.out.println("Date2 is before date1: " + before);

	      boolean after = date2.after(date1);
	      System.out.println("Date2 is after date1 : " + after);

	      int compare1 = date1.compareTo(date2);
	      int compare2 = date2.compareTo(date1);
	      int compare3 = date1.compareTo(date1);
	      System.out.println("campare date1 to date2:" + compare1);
	      System.out.println("campare date2 to date1:" + compare2);
	      System.out.println("campare date1 to date1:" + compare3);

              boolean c1 = date1.equals(date2);
	      System.out.println(" Are Dates equal??? :" + c1);

	      long d1 = date1.getTime();
System.out.println("If date is 3-8-1990, " + d1 + " this much time have passed.");
	}
}
  • Above example demonstrate the usage of methods like before (), after (), compare of Class Date (), equals () and get time ().
  • Date date1 = new Date (90, 8, 3); line creates the Date Object, date1 which accepts the date format as year, month, and date and similarly next line does the same thing.
  • boolean before = date2.before(date1); line checks whether the date2 is before the date1 or not, and returns boolean true or false.
  • boolean after = date2.after(date1); line checks whether the date2 is after the date1 or not, and returns boolean true or false.
  • Below three lines makes the comparison of date 1 and date 2 in three different ways i.e.
    • int compare1 = date1.compareTo(date2);
    • int compare2 = date2.compareTo(date1);
    • int compare3 = date1.compareTo(date1);
  • Comparison output
    • -1, when date1 is greater than date2.
    • 1, when date1 is less than date2.
    • 0, when date1 is equal to date 1.
  • boolean c1 = date1.equals(date2); this methods checks for the equality of two specified dates, date 1 and date 2. It returns the boolean true if dates are equal else returns boolean false.
  • long d1 = date1.getTime(); statement returns the time in terms of number of seconds elapsed since Jan 1, 1970, 00:00:00 GMT.2

When you run the above example, you would get the following output:

Java Date Example

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*