Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-28-2008, 10:02 AM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,691
Rep Power: 5
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default Comparing dates
The class below shows how to compare two dates.

Code:
public class TestDate {
  public static void main(String args[]){
    String DATE_FORMAT = "yyyy-MM-dd";
    java.text.SimpleDateFormat sdf =
          new java.text.SimpleDateFormat(DATE_FORMAT);
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    // remember months are zero-based : 0 jan 1 feb ...
    c1.set(1999, 11 , 31);
    c2.set(1999, 0 , 30);

    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.println(sdf.format(c2.getTime()));
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Compare two Dates Java Tip java.util 1 06-24-2008 08:05 AM
No fo days between two dates Java Tip Java Tips 0 01-28-2008 10:06 AM
Inbetween Dates Rageagainst20 New To Java 4 12-19-2007 06:24 AM
help with dates and time osval New To Java 3 12-12-2007 01:41 PM
differences between 2 dates cecily New To Java 1 08-02-2007 06:37 PM


All times are GMT +2. The time now is 04:52 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org