java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.util arrow Compare two dates
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3947
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Compare two dates E-mail
User Rating: / 78
PoorBest 

This tips shows how to compare two dates in Java.

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

public class TestDate {
  public static void main(String args[]){
    TestDate a = new TestDate();
    }

  TestDate() {

    String DATE_FORMAT = "yyyy-MM-dd";
    java.text.SimpleDateFormat sdf = 
          new java.text.SimpleDateFormat(DATE_FORMAT);
    Calendar c1 = Calendar.getInstance()
    Calendar c2 = Calendar.getInstance()
    c1.set(199912 31)
    c2.set(199930)
    
    System.out.print(sdf.format(c1.getTime()));
    
    if (c1.before(c2)) {
       System.out.print(" is before ");
       }
    if (c1.after(c2)) {
       System.out.print(" is after ");     
      }  
    if (c1.equals(c2)) {
       System.out.print(" same as ");     
      }  
    System.out.print(sdf.format(c2.getTime()));
    }
  }

 Related Tips

 
< Prev   Next >
 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.