Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Comparing two Dates in Java 
 

In this example we are going to compare two date objects using java.util.Date class. For comparing dates, we will be using compareTo() method.

 

Comparing two Dates in Java

                         

In  this example we are going to compare two date objects using java.util.Date class. For comparing dates, we will be using compareTo() method. The compareTo() method returns integer value after comparing the dates.

In CompareDate example class we have defined firstDate object and then we used the Thread.Sleep(1000) method to make thread to sleep for 1 second and then it creates new object secondDate after. So that second date is created after one second of creation of firstDate. Now in our example program we can compare these two dates using compareTo() method.

firstDate.compareDate(secondDate);

  • firstDate.compareTo(secondDate) will return "0" when both dates are exactly equal
  • firstDate.compareTo(secondDate) will return "1" when firstDate is greater than secondDate and
  • firstDate.compareTo(secondDate) will return "-1" when firstDate is less than secondDate.

Here is the full example code of CompareDate class as follows:

CompareDate.java

import java.util.Date;
public class CompareDate{

  public static void main(String[] args) {
       Date firstDate = new Date();
        try{
        Thread.sleep(1000);
            }catch(Exception e){
        }
    Date secondDate = new Date();
    System.out.println("FirstDate:="+firstDate);
  System.out.println("SecondDate:="+secondDate);
  if(firstDate.compareTo(secondDate0)      
      System.out.println("Second Date is initialized before First Date");
    else if(firstDate.compareTo(secondDate0)
      System.out.println("Second Date is initialized after First Date");
  else
    System.out.println("First Date and Second Date are equal");
  }
}

Output:

C:\DateExample>javac CompareDate.java

C:\DateExample>java CompareDate
FirstDate:=Wed Oct 08 17:22:05 GMT+05:30 2008
SecondDate:=Wed Oct 08 17:22:06 GMT+05:30 2008
Second Date is initialized after First Date

Download Source Code

                         

» View all related tutorials
Related Tags: c class orm date form diff format locale using this simple local show if for example program to ram exam

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

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

i am very glad to see such examples and pls keep on that!
Best Regards!

tirfe

Posted by tirfe on Sunday, 12.21.08 @ 23:15pm | #83021

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

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 | Flex 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.