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.
JANUARY=0. So month '1' is FEBRUARY. Plus '1' is MARCH. Hence 2000-03-02.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
Carey Brown wrote:
JANUARY=0. So month '1' is FEBRUARY. Plus '1' is MARCH. Hence 2000-03-02.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
Should have written:
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?
Campbell Ritchie wrote:One reason for abandoning Calendar and Date is that the month numbers in Calendar are unintuitive.
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.Mike London wrote:. . . I didn't use the Enumeration to get Calendar.JANUARY.
On behalf of the other people who gave you more of an answer than I did:-
Thanks,
-- mike
Campbell Ritchie wrote:
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.Mike London wrote:. . . I didn't use the Enumeration to get Calendar.JANUARY.
On behalf of the other people who gave you more of an answer than I did:-
Thanks,
-- mike
That's a pleasure
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
|