Comparing Dates in Java

In this section, we will show you how to compare two given dates.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Comparing Dates in Java

     

This example illustrates you how to compare two dates. Generally dates are calculated in milliseconds. In this section, we will show you how to compare two given dates. To compare dates the given program is helpful for application development. You can easily use the code for comparing dates.

Program Description:

This program describes how the dates are compared. Here, date is mentioned in the code which has to be compared from the current date. It will show  the result whether the current date is greater, equal or less than the given date.

Code Description:

Some built-in methods, fields and APIs are explained as follows:

Calendar.getInstance():
Usually this provides the instance of the Calendar class. It returns the calendar for the default time zone and locale.

Calendar.set(2000, Calendar.JUNE, 29):
This method sets the default date to the calendar passing three arguments like year, month and day for the specific object of the Calendar class.

cal.before(currentcal):
Usually this method returns a boolean value true, if the date mentioned for the cal object is previous date of the mentioned date for the currentcal object of the Calendar class, otherwise it returns the boolean value false.

cal.after(currentcal):
Usually this method returns a booean value true, if the date mentioned for the cal object is later date from the mentioned date for the currentcal object of the Calendar class, otherwise it returns the boolean value false.

new SimpleDateFormat("dd/MM/yyyy"):
Above creates the new instance of the SimpleDateFormat class of the java.text.*; package. It specifies the format for the date. The format is mentioned in the constructor of the SimpleDateFormat class which is the String type argument.

SimpleDateFormat.format(date):
It formats the date in the specified format.

Calendar.getTime():
This method returns the default date in time.

Here is the code of the program:

import java.util.*;
import java.text.*;

public class CompareDate{
  public static void main(String args[]){
  Calendar cal = Calendar.getInstance();
  Calendar currentcal = Calendar.getInstance();
  cal.set(2000, Calendar.JUNE, 29);
  currentcal.set(currentcal.get(Calendar.YEAR),
currentcal.get
(Calendar.MONTH), currentcal.get(Calendar.DAY_OF_MONTH));
  if(cal.before(currentcal))
  System.out.print("Current date(" new SimpleDateFormat("dd/MM/yyyy").
format
(currentcal.getTime()) ") is greater than the given date " new
SimpleDateFormat("dd/MM/yyyy").format(cal.getTime()));
  else if(cal.after(currentcal))
  System.out.print("Current date(" new SimpleDateFormat("dd/MM/yyyy").
format
(currentcal.getTime()) ") is less than the given date " new 
SimpleDateFormat("dd/MM/yyyy").format(cal.getTime()));
  else
  System.out.print("Both date are equal.");
  }
}

Download this example.

Advertisements

Ads
Share on Google+Share on Google+

Comparing Dates in Java

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Comparing Dates in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
dylan
January 3, 2012
how to call a date on hashtag

how can i call a date on a hashtag
Suraj
January 16, 2012
No of days between two Dates

Friends, Please help me to find the number of dates between two calender dates in Java. thanks, SURAJ
sunil sharma
February 14, 2012
regarding to this program

This is really a good program to illustrate the basic calendar class thanks

Ads

 

Ads