This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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();
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.
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.