Hema
Hema

Reputation: 39

Comparison between two dates not showing correct result

In my android app, I need to find which date is greater or smaller.I have used the following code.This code works good but sometimes it shows wrong output.Pls help

    String strDate1 = "3/9/2013";   
    String strDate2 = "4/6/2013";   

    SimpleDateFormat sdf = new SimpleDateFormat( "m/d/yyyy" );   
    java.util.Date d1;
    java.util.Date d2;
    try {
        d1 = sdf.parse( strDate1 );
        d2 = sdf.parse( strDate2 );   
        if  ( d1.after(d2))  
        {   
              System.out.println( "d1 is after d2" ); 
        }   
        else if  ( d1.before(d2) )
        {   

           System.out.println( "d1 is before d2" ); 
        }   
        else  
        {   
           System.out.println( "d1 is equal to d2" );   
        }  

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   

For the above code i got the output as "d1 is after d2".But the real thing is "d1 is before d2".Pls suggest if I did any mistake on this.

Upvotes: 2

Views: 127

Answers (5)

Basil Bourque
Basil Bourque

Reputation: 340230

tl;dr

Period
.between (
    LocalDate.parse ( "3/9/2013" ,  DateTimeFormatter.ofPattern( "M/d/uuuu" ) ) ,
    LocalDate.parse ( "4/6/2013" ,  DateTimeFormatter.ofPattern( "M/d/uuuu" ) ) ,
)
.isNegative ()

java.time

In modern Java (& Kotlin) on Android, use the java.time classes that supplanted the terribly-flawed legacy date-time classes. Never use Calendar, Date, etc.

Android 26+ comes with an implementation of java.time. For earlier Android, the latest tooling provides most of the java.time functionality via “API desugaring”.

LocalDate

You said:

String strDate1 = "3/9/2013";

String strDate2 = "4/6/2013";

For a date-only value, without time-of-day, and without time zone or offset-from-UTC, use java.time.LocalDate.

FYI, better to use standard ISO 8601 formatting for your textual date-time values, rather than some custom or localized text. For a date-only value, that would be YYYY-MM-DD.

For your custom format, define a formatting pattern to match.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "M/d/uuuu" ) ;

Parse.

LocalDate x = LocalDate.parse( strDate1 , f ) ;
LocalDate y = LocalDate.parse( strDate2 , f ) ;

To handle invalid inputs, trap for DateTimeParseException.

Compare:

boolean isXBeforeY = x.isBefore ( y ) ;
boolean isXAfterY = x.isAfter ( y ) ;
boolean isXEqualToY = x.isEqual ( y ) ;

Calculate elapsed time.

Period elapsed = Period.between( x , y ) ;

You can check to see which date comes before the other by calling Period#isNegative & Period#isZero.

Upvotes: 1

eatSleepCode
eatSleepCode

Reputation: 4637

SimpleDateFormat this shows small m is for minute and capital M is for month so try changing it.

Upvotes: 0

spiritwalker
spiritwalker

Reputation: 2257

try change your date format to the following

SimpleDateFormat sdf = new SimpleDateFormat( "M/d/yyyy" );

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

You need to get months using M not minutes which is provided by m.

SimpleDateFormat sdf = new SimpleDateFormat( "M/d/yyyy" );   

Upvotes: 0

PermGenError
PermGenError

Reputation: 46438

you used wrong format for month.

SimpleDateFormat sdf = new SimpleDateFormat( "M/d/yyyy" );

m is for minutes in hours M is for Month in year

Upvotes: 5

Related Questions