views:

123

answers:

5

Hi All,

I want to get the difference between two Java Date objects. I've used Joda-Time library. But the problem is that I'm getting the Days greater difference than that of actual day difference.

Here's my code snippet:

DateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");

Date someDate=new Date();
Date today = Calendar.getInstance().getTime();

try     {
    someDate = formatter.parse("06/22/2010");
}
catch(ParseException pe)    {
    System.out.println("Parser Exception");
}

int days = Days.daysBetween(new DateTime(someDate), new DateTime(today)).getDays();

System.out.println(" Days Between " + someDate + " : " + today + " - " + days);

Here's my output:

 Days Between Fri Jan 22 00:06:00 IST 2010 : Sun Jul 25 19:27:01 IST 2010 - 184

Here, Why does it takes "06/22/2010" as Jan 22? Does anyone face similar problem?

Help me friends.. Thanx in advance..

+3  A: 

Month is MM

In your case:

DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");

Cambium
+1  A: 

mm => minutes, not months - you need MM for months - that'll resolve your Jan problem!

Will A
+2  A: 

Your pattern is slightly defective. mm is parsed as minutes in hour, you're looking for MM which is month of year.

Esko
+9  A: 

It seems like mm refers to minutes, not months. Please check here to see the list of appropriate lettering :)

npinti
A: 

any of the answers above will do. That happened to me last month. :P

If you agree with any of the above answers you should be upvoting them instead...
BoltClock