Results 1 to 2 of 2
  1. #1
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    10

    Default How to Compare two Dates

    This example code uses the Calendar class to compare two dates.

    Java Code:
    import java.util.*;
    import java.text.*;
    
    public class DemoDate {
      public static void main(String args[]){
        DemoDate a = new DemoDate();
        }
    
      DemoDate() {
    
        String DATE_FORMAT = "yyyy-MM-dd";
        java.text.SimpleDateFormat sdf = 
              new java.text.SimpleDateFormat(DATE_FORMAT);
        Calendar calender1 = Calendar.getInstance();
        
        Calendar calendar2 = Calendar.getInstance();
        calender1.set(2006, 04 , 28); 
        calendar2.set(2006, 04 , 28); 
        
        System.out.print(sdf.format(calender1.getTime()));
        
        if (calender1.before(calendar2)) {
           System.out.print(" is before ");
           }
        if (calender1.after(calendar2)) {
           System.out.print(" is after ");     
          }  
        if (calender1.equals(calendar2)) {
           System.out.print(" same as ");     
          }  
        System.out.print(sdf.format(calendar2.getTime()));
        }
      }

  2. #2
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    Kool,

    how about gettin the diff. between

    String startDate = "1/23/2008" ; // MM/DD/YYYY
    String endDate = "5/20/2018" ; // MM/DD/YYYY

    String startTime = "10:00 PM" ; // hh:mm a
    String endTime = "1:00 AM" ; // hh:mm a

    Difference b/w, startDate + startTime and endDate + endTime :)
    i am the future

Similar Threads

  1. No fo days between two dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 10:06 AM
  2. Comparing dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 10:02 AM
  3. Inbetween Dates
    By Rageagainst20 in forum New To Java
    Replies: 4
    Last Post: 12-19-2007, 06:24 AM
  4. help with dates and time
    By osval in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 01:41 PM
  5. differences between 2 dates
    By cecily in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •