GregorianCalendar Leap Years?
Does GregorianCalendar take into account Leap years when you use getTimeInMillis() ?
for the code :
Scanner in =new Scanner(new File("leap.txt"));
int y1= in.nextInt();
int m1= in.nextInt();
int d1= in.nextInt();
int y2= in.nextInt();
int m2= in.nextInt();
int d2= in.nextInt();
long da1 =new GregorianCalendar(y1,m1-1,d1).getTimeInMillis();
long da2 =new GregorianCalendar(y2,m2-1,d2).getTimeInMillis();
System.out.println(da1);
GregorianCalendar d =new GregorianCalendar();
d.setTimeInMillis(da2-da1);
System.out.println(d.get(Calendar.YEAR)-1970+"/"+
d.get(Calendar.MONTH)+"/"+d.get(Calendar.DAY_OF_MONTH));
When i input
1920 12 10
1990 1 15
I get 69/1/5
Thats the difference in year/month/day but since there are leap years, the day should be bigger than 5.
What am i doing wrong?
Yes, GregorianCalendar always accounts for leap years no matter what sensible thing you are doing.
But when you start doing whatever it is you are doing, all bets are off.
You find the number of milliseconds between two dates, apparently. And then you create a date which is that number of milliseconds after January 1, 1970. Then you do something that claims to convert that to years, months, and days.
Your complaint about leap years highlights the reason your code is flawed. Suppose your two dates were 1 January 1920 and 1 January 1921. There are 366 days between those dates because 1920 was a leap year. But if you add 366 days to 1 January 1970 you get 2 January 1971 because 1970 was not a leap year, so your code is going to return 1 year, 0 months, 1 day.
In general trying to convert milliseconds into years, months, and days is an undefined exercise because months have different lengths and so do years. Your solution applies a definition but evidently not the definition you want.
When you want a difference in months do you always want the first month to have 31 days and the second month have 28 days? My guess is that you don't. So stop a minute and work out what your definition is really.