Given:
private Calendar calendarInstance = Calendar.getInstance(); public long inMillis() { calendarInstance.set(year, month, day, hour, min); return calendarInstance.getTimeInMillis(); }
As i understand it, the result comes back with time since the epoch, in milliseconds
The current time as UTC milliseconds from the epoch.
Given that my test always sets the objects the same, why are results coming up different as time goes by?
detailedMoment = new MomentInTime(2012, 11, 1, 19, 9); detailedMoment.inMillis() // gives different results as time passes by
UPDATE:
I continue to second guess myself due to
For the same time period i get
1_351_796_940 // http://www.epochconverter.com 1_354_410_540 // my number
Advertisement
Answer
I think you should use clear(). If you do that it will return you the exact number of miliseconds each time.
public long inMillis() { calendarInstance.clear(); calendarInstance.set(year, month, day, hour, min); return calendarInstance.getTimeInMillis(); }
From Java doc
Sets all the calendar field values and the time value
(millisecond offset from the Epoch)
of this Calendar undefined. This means that isSet() will return false for all the calendar fields, and the date and time calculations will treat the fields as if they had never been set. A Calendar implementation class may use its specific default field values for date/time calculations. For example, GregorianCalendar uses1970
if the YEAR field value is undefined.
A Sample program
public class MomentInTime { private static Calendar calendarInstance = Calendar.getInstance(); public static long inMillis() { calendarInstance.clear(); calendarInstance.set(2012, 10, 1, 19, 9); return calendarInstance.getTimeInMillis(); } public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 10; i++) { System.out.println(inMillis()/1000); Thread.sleep(300); } } }
Output:
1351777140