Well, you can't just always add 365 days, because some years (leapyears)
have 366 days.
The point is, if you add a year and you are in a leapyear before 29-02 you
have to add 366 to get to the correct day the next year (20-02-2004 + 366 =
20-02-2005).
And after 29-02 you have to add 365 days to get to the correct day
(20-03-2004 + 365 = 20-03-2005).
But what should be added when you are ON 29-02? For that I'm not quite
sure... it seems Java and Joda Time disagree on this. And I don't have the
ISO to confirm what the correct method is.
Op 7 okt. 2009 10:07 schreef Ken Egervari <ken.egerv...@gmail.com>:
I can't speak for the Joda Time programmers, but I think their
implementation is what I would expect it to be.
If you want your semantics, then you should add 365 days instead of 1
year, that way it's always 365 days. The 1 year is implemented correctly
because if you added 28 years, then there would be many days difference
from the starting point, and this would actually be quite weird. If you
want your semantics, then add the exact days.
Ken
--
Brand New Song! Listen NOW!
http://dl.getdropbox.com/u/443421/Ken_Egervari_-_When_It_Rains.mp3
My Original Music: http://www.soundclick.com/KenEgervari
My Humorous Speeches: http://www.youtube.com/user/egervari
My Facebook:
http://www.new.facebook.com/profile.php?id=716270499/profile.php?id=716270499
On Wed, Oct 7, 2009 at 3:43 AM, Roy van Rijn roy.van.r...@gmail.com>
wrote:
Hi all,
Not so long ago we decided to ditch all the Calendar stuff and use
Joda Time instead. But during testing our testers found a "bug" in de
code. It has to do with leapdays and adding years. The main question
is:
If today is 29-02, what day is it when I add one year? Is it 28-02 or
01-03?
We decided to try the Java GregorianCalendar against the two most
logical Joda Time chronologies:
-------------------------
Chronology greChrono = GregorianChronology.getInstance();
Chronology isoChrono = ISOChronology.getInstance();
DateMidnight gregorian = new DateMidnight(2004,2,29, greChrono );
DateMidnight iso = new DateMidnight(2004,2,29, isoChrono); //same as
default
GregorianCalendar cal = (GregorianCalendar)gregorian.toCalendar(null);
//Add a year to gregoriancalendar:
cal.roll(GregorianCalendar.YEAR, true);
System.out.println("Gregorian Calendar:" + cal.getTime()); //Prints:
Gregorian Calendar:Tue Mar 01 00:00:00 CET 2005
//Add year to Joda:
gregorian = gregorian.plusYears(1);
System.out.println("JodaTime Gregorian: " + gregorian.toDate());
//Prints: JodaTime Gregorian: Mon Feb 28 00:00:00 CET 2005
iso = iso.plusYears(1);
System.out.println("JodaTime ISO: " + iso.toDate()); //Prints:
JodaTime ISO: Mon Feb 28 00:00:00 CET 2005
-------------------------
What is the correct method?
Our testers think 01-03 is correct :-(
Roy
------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Joda-interest mailing list
Joda-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/joda-interest
------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Joda-interest mailing list
Joda-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/joda-interest