Results 1 to 5 of 5
  1. #1
    16mydream is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Nitpick: leading zeros in literal values are pointless.

    But as Fubar pointed out if you use the constants this is not an issue.

  4. #4
    16mydream is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    thank you both very much!
    the difference now matches up =)

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    By the way, when in doubt read the API:

    month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.

Similar Threads

  1. Replies: 0
    Last Post: 10-13-2010, 06:26 PM
  2. getIp Function finding wrong type
    By Dom in forum New To Java
    Replies: 9
    Last Post: 06-29-2010, 11:13 AM
  3. Replies: 4
    Last Post: 07-23-2009, 09:45 PM
  4. How to set values with Calendar class ?
    By mohiit in forum New To Java
    Replies: 3
    Last Post: 05-14-2008, 04:21 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •