• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

DateTime Math does not agree

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The two ways of getting a date one month in the future below do not give the same dates.

I'm confused then by what is a date one month in the future.

Example 1:


Prints 2000-02-29


-----------------------------------------------

But, ...

       Calendar zcal = Calendar.getInstance();
       zcal.set(Calendar.DAY_OF_MONTH, 31);
       zcal.set(Calendar.MONTH, 1);
       zcal.set(Calendar.YEAR, 2000);
       zcal.add(Calendar.MONTH, 0); // add a month

Prints: 2000-03-02

What?!

How can these values be different? Which is correct? If both are "OK", then how do you decide which to use?

Thanks in advance,

- mike
 
Saloon Keeper
Posts: 15714
367
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to ask yourself, what does it mean to add a month to a date?

Do you add a fixed amount of time? Do you just increment the month field by one? If the latter, what do you do if you end on an invalid date (such as 31st of february)?

There is no single correct interpretation. Both classes have their own interpretation of what it means to add a month. Those interpretations may or may not be what you want, but you first have to explain what it is you're trying to achieve.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You have to ask yourself, what does it mean to add a month to a date?

Do you add a fixed amount of time? Do you just increment the month field by one? If the latter, what do you do if you end on an invalid date (such as 31st of february)?

There is no single correct interpretation. Both classes have their own interpretation of what it means to add a month. Those interpretations may or may not be what you want, but you first have to explain what it is you're trying to achieve.



It's odd that I have to parse what it means to add a month, at first thought, unambiguous, but I suppose this is an edge case where that addition is open to interpretation.

Thanks,
 
Bartender
Posts: 10893
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike London wrote:Hello,

The two ways of getting a date one month in the future below do not give the same dates.

I'm confused then by what is a date one month in the future.

Example 1:


Prints 2000-02-29


-----------------------------------------------

But, ...

       Calendar zcal = Calendar.getInstance();
       zcal.set(Calendar.DAY_OF_MONTH, 31);
       zcal.set(Calendar.MONTH, 1);
       zcal.set(Calendar.YEAR, 2000);
       zcal.add(Calendar.MONTH, 0); // add a month

Prints: 2000-03-02

What?!

How can these values be different? Which is correct? If both are "OK", then how do you decide which to use?

Thanks in advance,

- mike

JANUARY=0. So month '1' is FEBRUARY. Plus '1' is MARCH. Hence 2000-03-02.

Should have written:
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Mike London wrote:Hello,

The two ways of getting a date one month in the future below do not give the same dates.

I'm confused then by what is a date one month in the future.

Example 1:


Prints 2000-02-29


-----------------------------------------------

But, ...

       Calendar zcal = Calendar.getInstance();
       zcal.set(Calendar.DAY_OF_MONTH, 31);
       zcal.set(Calendar.MONTH, 1);
       zcal.set(Calendar.YEAR, 2000);
       zcal.add(Calendar.MONTH, 0); // add a month

Prints: 2000-03-02

What?!

How can these values be different? Which is correct? If both are "OK", then how do you decide which to use?

Thanks in advance,

- mike

JANUARY=0. So month '1' is FEBRUARY. Plus '1' is MARCH. Hence 2000-03-02.

Should have written:



Thanks Cary,

That's very helpful.

Now the plusMonths(1) and Calendar match :2/29/2000!

A third-party program I'm using (FileMaker) seems to have a different algorithm.

This non-OO date function call below returns 3-2-2000 and seems to be the only option for adding dates there.

Date("01"+1 ; "31" ; "2000") = 3-2-2000

Based on now I have two other standard Date APIs that return 2/29/2000, which is what I expect, well, I don't know what to conclude about FileMaker's "data math".

By the way, in Excel, if you add 1 to January and you don't use the EDATE() function to add months (where you get 2/29/2000), you'll also get 3/2/2000.

Can be very confusing!

--

Thanks very much.

- mike
 
Carey Brown
Bartender
Posts: 10893
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are interfacing Java to some 3rd party package (e.g. FileMaker), then presumably there is a way to get a date back out of the 3rd party program. A date could take several forms: a String, a Date (or related) object, or some number of seconds or milliseconds since some epoch. What does FileMaker's API offer you?
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:If you are interfacing Java to some 3rd party package (e.g. FileMaker), then presumably there is a way to get a date back out of the 3rd party program. A date could take several forms: a String, a Date (or related) object, or some number of seconds or milliseconds since some epoch. What does FileMaker's API offer you?



The problem was comparing dates Java created (Period.between(....)) with FileMaker's date calculations. FileMaker got 3/2/2000 for January 31, 2000 + 1 month where Java got the expected 2/29/2000.  It seems FileMaker makes different assumptions near leap year than every other product I tried (Java, JavaScript's "adjust" function, Excel, and a Web site I found that got the same results as the other three for date math).

FileMaker's date API is nearly non-existent. Just a few functions. Compared to Java, well, .... Fugetaboutit.

Thanks again Cary!

-- mike
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One reason for abandoning Calendar and Date is that the month numbers in Calendar are unintuitive.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:One reason for abandoning Calendar and Date is that the month numbers in Calendar are unintuitive.



Totally agree. I'm completely on the Java 8+ DateTime API now, but needed another test case today with this weird date math calculation from the third party application. As you can see above, to underscore your point, I made that "off-by-one" error that gave me the incorrect date since I didn't use the Enumeration to get Calendar.JANUARY.

Thanks,

-- mike
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike London wrote:. . . I didn't use the Enumeration to get Calendar.JANUARY.

Unfortunately, it isn't an enumeration (small e; an Enumeration with large E is something different), nor an enum, nor anything enumerated; it is simply ints as constants in the class. Had it been enumerated, as this is, you wouldn't have the problem of entering 2 and getting MARCH. You would have a compiler error from anything requiring that enum.



Thanks,

-- mike

On behalf of the other people who gave you more of an answer than I did:-

That's a pleasure
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Mike London wrote:. . . I didn't use the Enumeration to get Calendar.JANUARY.

Unfortunately, it isn't an enumeration (small e; an Enumeration with large E is something different), nor an enum, nor anything enumerated; it is simply ints as constants in the class. Had it been enumerated, as this is, you wouldn't have the problem of entering 2 and getting MARCH. You would have a compiler error from anything requiring that enum.



Thanks,

-- mike

On behalf of the other people who gave you more of an answer than I did:-

That's a pleasure



Thanks Campbell. Good to know. And, BTW, you've been very helpful.

- mike
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic