I’m trying to compare two millisecond values in Java. One from a calendar, and one from System.currentTimeMillis(). However, it seems the value from the calendar is always far larger than the one from System.currentTimeMillis() at the correct time.
To build the calendar, I’m parsing a date string in the format dd/MM/yyyy HH:mm and putting it’s consitutuent parts in to a calendar (try-catch omitted for brevity).
Calendar obCal = null;
//Exception here shows invalid format
DateFormat DF = new SimpleDateFormat("dd/MM/yyyy HH:mm");
DF.parse(Date);
//Example string: 29/11/2011 16:30
String[] parts = Date.split("/");
obCal = Calendar.getInstance();
int Y = Integer.parseInt(parts[2].split(" ")[0]);
int m = Integer.parseInt(parts[1]);
int d = Integer.parseInt(parts[0]);
int H = Integer.parseInt(parts[2].split(" ")[1].split(":")[0]);
int M = Integer.parseInt(parts[2].split(" ")[1].split(":")[1]);
obCal.set(Calendar.YEAR, Y);
obCal.set(Calendar.MONTH, m);
obCal.set(Calendar.DAY_OF_MONTH, d);
obCal.set(Calendar.HOUR_OF_DAY, H);
obCal.set(Calendar.MINUTE, M);
obCal.set(Calendar.SECOND, 0);
obCal.set(Calendar.MILLISECOND, 0);
Inspecting the calendar after this shows me it is receiving the correct date and time. I then get the millisecond value since the epoch from obCal.getTimeInMillis() and store it as a number of seconds as a long
long stamp = obCal.getTimeInMillis() / 1000L;
When I reach the time represented by the date string, and compare the timestamp from the calendar to timestamp System.currentTimeMillis(), I find the former is far larger. The difference between the current time and the calendar time (curTime-calTime) is, usually, somewhere in the region of -2,500,000
What could be causing this?
Thanks
Month is 0-based so when you do
obCal.set(Calendar.MONTH, m);you’re off by 1 month.