Results 1 to 5 of 5
- 02-16-2011, 02:38 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
getTimeInMillis() function of the Calendar class returns wrong values
i'm using the getTimeInMillis() function from the Calendar class to calculate the number of milliseconds from the epoch to a particular day. however, it seems as if the function is returning wrong values by assuming that every month has 31 days.
a sample code I tested is as follows:
Java Code:long mili; Calendar now = new GregorianCalendar(); now.set(2011, 02, 28); mili = now.getTimeInMillis(); System.out.println(mili); now.set (2011, 03, 01); mili = now.getTimeInMillis(); System.out.println(mili); now.set(1970, 1, 1); mili = now.getTimeInMillis(); System.out.println(mili);
the output i got was 1301280211567 for 2011/02/28 and 1301625811567 for 2011/03/01, there is a four day difference in the milliseconds, when there should only have been one.
also, the output i got for 1970/01/01 is 2688211567. shouldn't this have been zero if getTimeInMilils() calculates starting from the epoch which is exactly 1970/01/01?
is there something i'm not doing correctly or is this really a bug in the function? if it is, is there any other way i can calculate the number of milliseconds to a certain day?
thanks!Last edited by 16mydream; 02-16-2011 at 04:23 AM. Reason: SOLVED
-
Don't use numbers for months but instead use the Calendar constants. This way it won't matter that months are 0-based (January is 0, Feb is 1, March is 2, ...), and you won't get tripped up by this.
- 02-16-2011, 03:19 AM #3
Nitpick: leading zeros in literal values are pointless.
But as Fubar pointed out if you use the constants this is not an issue.
- 02-16-2011, 04:21 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
thank you both very much!
the difference now matches up =)
- 02-16-2011, 04:31 AM #5
Similar Threads
-
Eclipse creates web service that returns an object but something's wrong...!!!
By nikos in forum EclipseReplies: 0Last Post: 10-13-2010, 06:26 PM -
getIp Function finding wrong type
By Dom in forum New To JavaReplies: 9Last Post: 06-29-2010, 11:13 AM -
Simple search-function: what am I doing wrong?
By Bas in forum New To JavaReplies: 4Last Post: 07-23-2009, 09:45 PM -
How to set values with Calendar class ?
By mohiit in forum New To JavaReplies: 3Last Post: 05-14-2008, 04:21 AM -
Error: Cannot access protected member long getTimeInMillis() in class Calendar
By cachi in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 07:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks