Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: getTimeInMillis() from the Calendar class returns wrong values

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default getTimeInMillis() from 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:
    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!


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: getTimeInMillis() from the Calendar class returns wrong values

    problem solved: should use class provided constants instead of numbers for the months

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: getTimeInMillis() from the Calendar class returns wrong values

    I think it has the months indexed, meaning January is 0 and December is 11. Not entirely sure though. Either way, class provided constants are always the easiest way to go.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Replies: 1
    Last Post: December 22nd, 2011, 09:55 AM
  2. [SOLVED] Using values set in one class in another
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 23rd, 2011, 10:34 PM
  3. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 09:51 PM
  4. How web service returns xml?
    By nikos in forum Web Frameworks
    Replies: 2
    Last Post: October 7th, 2010, 12:50 PM
  5. I'm having trouble with date and calendar class
    By kiph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 02:56 AM