From: Lars Uffmann on 29 Nov 2009 16:11 Simple code snippet: =================== Calendar date = Calendar.getInstance(); date.set(Calendar.YEAR, 2007); date.set(Calendar.MONTH, 4); date.set(Calendar.DAY_OF_MONTH, 30); date.add(Calendar.DAY_OF_YEAR, 1); =================== After this manipulation, the date reads out as 2007-04-31 - reading it with date.get(Calendar.YEAR), date.get(Calendar.MONTH) and date.get(Calendar.DAY_OF_MONTH). Same behaviour when calling add with field Calendar.DAY_OF_MONTH. This is with Compiler compliance 1.5 and 1.6 in Sun Java. Is anyone able to tell me how I can tell this Calendar class to do proper date calculation? I don't want to do everything by hand, accounting for leap years - I just want to add a certain amount of days to a date and get the date of the result... Thanks for any hints! Lars
From: Roedy Green on 1 Dec 2009 03:54 On Sun, 29 Nov 2009 22:11:22 +0100, Lars Uffmann <aral(a)nurfuerspam.de> wrote, quoted or indirectly quoted someone who said : > >Is anyone able to tell me how I can tell this Calendar class to do >proper date calculation? One way is to use BigDate. I was so frustrated with the bugs and "features" of Date I wrote BigDate. The main difference is BigDate works on pure dates, not timestamps. http://mindprod.com/products1.html#COMMON11 -- Roedy Green Canadian Mind Products http://mindprod.com I mean the word proof not in the sense of the lawyers, who set two half proofs equal to a whole one, but in the sense of a mathematician, where half proof = 0, and it is demanded for proof that every doubt becomes impossible. ~ Carl Friedrich Gauss
From: Dr J R Stockton on 2 Dec 2009 13:04 In comp.lang.java.programmer message <7ng5mrF3lbplvU1(a)mid.dfncis.de>, Sun, 29 Nov 2009 22:11:22, Lars Uffmann <aral(a)nurfuerspam.de> posted: >Is anyone able to tell me how I can tell this Calendar class to do >proper date calculation? I don't want to do everything by hand, >accounting for leap years - I just want to add a certain amount of days >to a date and get the date of the result... Gregorian Date day arithmetic is trivial if one has arithmetical routines to convert between a Y M D triple and a day-count. A variety of algorithms for that, with tests, in JavaScript, can be found in <URL:http://www.merlyn.demon.co.uk/daycount.htm> ; translation to Java should be easy for anyone who knows and can run Java. Any Java library with Date routines should provide, and document, corresponding conversions, carefully avoiding any need to determine Summer Time either implicitly or explicitly. Month arithmetic is trivial by converting to/from month-count, except that one must decide what to do if the starting day number is too large for the finishing month. Year arithmetic is trivial, except that one must decide what to do if the starting date is February 29 and the finishing year is not leap. Rarely will it be possible to code *significantly* briefer or faster. -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05. Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
From: Arne Vajhøj on 2 Dec 2009 19:24 Dr J R Stockton wrote: > In comp.lang.java.programmer message <7ng5mrF3lbplvU1(a)mid.dfncis.de>, > Sun, 29 Nov 2009 22:11:22, Lars Uffmann <aral(a)nurfuerspam.de> posted: > >> Is anyone able to tell me how I can tell this Calendar class to do >> proper date calculation? I don't want to do everything by hand, >> accounting for leap years - I just want to add a certain amount of days >> to a date and get the date of the result... > > Gregorian Date day arithmetic is trivial if one has arithmetical > routines to convert between a Y M D triple and a day-count. A variety > of algorithms for that, with tests, in JavaScript, can be found in > <URL:http://www.merlyn.demon.co.uk/daycount.htm> ; translation to Java > should be easy for anyone who knows and can run Java. > > Any Java library with Date routines should provide, and document, > corresponding conversions, carefully avoiding any need to determine > Summer Time either implicitly or explicitly. > > Month arithmetic is trivial by converting to/from month-count, except > that one must decide what to do if the starting day number is too large > for the finishing month. > > Year arithmetic is trivial, except that one must decide what to do if > the starting date is February 29 and the finishing year is not leap. > > Rarely will it be possible to code *significantly* briefer or faster. If you read the rest of the thread then you will see that the problem was not lack of functionality but the fact that the OP did not notice the fine print in the docs saying month's in Java is zero based so that month 4 is May. Arne
From: Arne Vajhøj on 2 Dec 2009 21:42 Roedy Green wrote: > On Sun, 29 Nov 2009 22:11:22 +0100, Lars Uffmann <aral(a)nurfuerspam.de> > wrote, quoted or indirectly quoted someone who said : >> Is anyone able to tell me how I can tell this Calendar class to do >> proper date calculation? > > One way is to use BigDate. I was so frustrated with the bugs and > "features" of Date I wrote BigDate. The main difference is BigDate > works on pure dates, not timestamps. > > http://mindprod.com/products1.html#COMMON11 When will we see: http://mindprod.com/dontwriteyourowncodeforfunctionalityinstandardjavaapi.html ? Arne
|
Next
|
Last
Pages: 1 2 Prev: Using examples in software development Next: Forcing garbage collection |