Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

thank u very much. and i have one doubt. if we want to give the date values from command prompt then what is the code we want to use to execute the same result??

kindly reply to this. its urgent

Posted by raju on Friday, 05.30.08 @ 09:39am | #61406

ur code is very helpful....thanks

Posted by marimuthu on Sunday, 05.18.08 @ 20:10pm | #60393

help me how to get the previous dates from the current date

Posted by swetha on Wednesday, 10.3.07 @ 13:31pm | #31300

Latest Searches:
Photoshop Effects Vale
Servlet Example To Dis
java programming defin
how to find the days b
ASP Ad Management CJ s
ASP Ad Management CJ s
Date Examples java
java input/output serv
draw triangle
ASP Ad Management CJ s
ASP Ad Management CJ s
ASP Ad Management CJ s
ASP Ad Management CJ s
jdbc insert
enter
data compression
iBatis Stored Procedur
examples with list
request.setAttribute
Photoshop Christmas Ba
Using Beans And Sessio
Photoshop Text Effects
reads data into text f
How to insert multiple
thread
logout ajax
array java
Date Difference
contact
java registration stud
get character in java
login window
procedure in mysql
EXT grid
Dependency list box in
action messages in str
3DS MAX Modeling Barre
JSTL API
builb.xml
velocity struts2
sitmesh
struct
login.jsp
define functions in js
EchoPoint
jsf comments
Create a 3D bar chart
FCKediter
enumerations
Web Service applicatio
Java: Sets
Insert Data into Datab
retrieve blob field in
foreach
swing components
compare two arraylist
jboss portal
Two Dimensional Array
JCalendar
3d max
Deitels
calling one jsp p
Filtering Selection fr
jsp Insert data in mys
Use of <fn:substringBe
poc dojo container
tree
java string
a comple progrma using
Photoshop Brushes Spar
jsp bean get property
url actions tag
delete data in mysql d
J2ME List Image
array in javascript
flex
dojo tree
regex progam to find v
enterprise portal
server side javascript
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.