aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Java Calendar Date compare Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Java Calendar Date compare" Watch "Java Calendar Date compare" New topic
Author

Java Calendar Date compare

Patrick Ferguson
Ranch Hand

Joined: Jun 15, 2004
Posts: 30
Well, after spending about two days trying to figure out how to compare to date objects to get the days different between them I'm coming to the experts for help.

Here is my code.

String sLlpc = String.valueOf(ldt_last_passwd_changed);
int d = Integer.valueOf(sLlpc.substring(5,7)).intValue();
int m = Integer.valueOf(sLlpc.substring(8,10)).intValue() - 1;
int y = Integer.valueOf(sLlpc.substring(0,4)).intValue();
System.out.println(d);
System.out.println(m);
System.out.println(y);
Calendar c1 = new GregorianCalendar(y, m, d);
System.out.println(c1.getTime().toString());
Calendar cNow = Calendar.getInstance();

System.out.println(cNow.getTime().toString());

long diffMillis = c1.getTimeInMillis()-cNow.getTimeInMillis();
System.out.println(diffMillis);
ll_password_age = diffMillis/(24*60*60*1000);
System.out.println("Password age: "+ll_password_age);

Here is the ouput to stdout.
2005-07-28 12:30:29,875 INFO [STDOUT] 7
2005-07-28 12:30:29,890 INFO [STDOUT] 27
2005-07-28 12:30:29,890 INFO [STDOUT] 2005
2005-07-28 12:30:29,890 INFO [STDOUT] Sat Apr 07 00:00:00 CDT 2007
2005-07-28 12:30:29,890 INFO [STDOUT] Thu Jul 28 12:30:29 CDT 2005
2005-07-28 12:30:29,890 INFO [STDOUT] 53350170110
2005-07-28 12:30:29,890 INFO [STDOUT] Password age: 617


As you can see I'm pushing in a date of 07/27/2005(not quite that format, but that shouldn't matter), and comparing it with today's date to get the number of days between the two.

It seems like what I have should work, but when I get the gregorian calendar instance is seems to interpret the int values incorrectly.

Does anyone know how I can fix this?

Thanks
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Patrick,

Accordingly to the code and the output values you've posted:

Hence you�ll basically instantiate a new Georgina calendar like this:

Because by default the GeorgianCalendar is lenient, it will try to convert the wrong month modulo 12, which is 4. This will correspond to April (Jan = 0, Feb = 1, etc). It will also add two more years to the initial 2005 and you get the 2007. If you�ll switch the m with d (and subtract 1 from the right month) then it will probably print the expected result.
Regards.


I think, therefore I exist -- Rene Descartes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Calendar Date compare