Results 1 to 10 of 10
  1. #1
    kasur is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default GregorianCalendar mishap

    Well, guys, hello out there! I have recently faced a problem concerning calculation of the number of days between specific dates (within creating certain logic). I have simplified and cut off redundant code to show you the core of the problem. Below you will find a snap of the code that produces different output results for consecutive dates.

    To demonstrate the trouble let us leave dateEnd unchanged and step up dateBegin from 2010,2,27 then 2010,2,28 then finally 2010,2,29
    So, it performs correctly and gives out an integer and exact number of days between dateEnd and dateBegin correspondently 280,00 279,00 but when dateBegin is 2010,2,29 there comes out 278,04167 as the number of days.

    Does anybody knows or has a slightest idea how to tackle this? Thanx.

    GregorianCalendar dateBegin=new GregorianCalendar(2010,2,29);
    GregorianCalendar dateEnd=new GregorianCalendar(2011,0,1);
    SimpleDateFormat dtf=new SimpleDateFormat("dd/MM/yyyy H:mS");
    DecimalFormat ddtf=new DecimalFormat("0.00000");
    System.out.println(dtf.format(dateBegin.getTime()) );
    System.out.println(dtf.format(dateEnd.getTime()));
    System.out.println(dateEnd.getTimeInMillis()-dateBegin.getTimeInMillis());
    System.out.println(ddtf.format((dateEnd.getTimeInM illis()-dateBegin.getTimeInMillis())/(24.0*60*60*1000L)));

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    When I run it I get this output:
    29/03/2010 0:0:0:0
    01/01/2011 0:0:0:0
    24019200000
    278.00000

  3. #3
    kasur is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    29/03/2010 0:0:0:0
    01/01/2011 0:0:0:0
    24022800000
    278,04167

    Should say that the difference is exactly 1 hour.... Rather interesting thing don't u think?

  4. #4
    kasur is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Running on two different machines - result the same!

    Eclipse Platform
    Version: 3.4.1
    Build id: M20080911-1700

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Ever heard of DST?

    db

  6. #6
    kasur is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Nope, but will look into it if u believe it is the reason

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Ever heard of DST?
    Yep, that's the cause: 24022800000 millis is not a multiple of 24 hours (one hour too many). The Gregorian calendar anticipated on DST adjustment.

    kind regards,

    Jos

  8. #8
    kasur is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Well, thanx everybody for yr help and time spent. Now when i am directed to read up on DTS, hope i'll find cure for that mishap. Thanx!

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Daylight Saving Time -- DST. Not DTS

    db

  10. #10
    kasur is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. GregorianCalendar help
    By Sturm in forum New To Java
    Replies: 0
    Last Post: 02-03-2010, 09:43 PM
  2. Problem with "GregorianCalendar"
    By tola.ch2004 in forum New To Java
    Replies: 2
    Last Post: 07-12-2007, 08:12 PM

Posting Permissions

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