• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you add one day to a Calendar?  RSS feed

 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java.util.Calendar, there are constants for MONTH and YEAR, so you can trivially add one day to a Calendar object.



same for YEAR. But there is no constant for DAY, so you can't just add one day to get the next day. So far, I've been adding 24 hours, which seems to be the same, but the code is ugly.

Is there a better way?
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try Calendar.DAY_OF_MONTH or Calendar.DATE?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:So far, I've been adding 24 hours, which seems to be the same, but the code is ugly.
Is there a better way?

Yes. Calendar.add(). And furthermore, I believe it takes DST into account.

Winston
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Yes. Calendar.add(). And furthermore, I believe it takes DST into account.


Things like DST are why Calendar was implemented to replace Date, which was mis-designed with a horrible API. But the primary storage of a Calendar object is just the elapsed time in milliseconds from a date long ago (1970). So there really is not a concept of DST without applying a Locale to it.

I know about Calendar.add, my question is does Calendar.DATE really mean Calendar.DAY?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:I know about Calendar.add, my question is does Calendar.DATE really mean Calendar.DAY?


Apparently so.

And when you have to add hours, let me warn you in advance not to confuse Calendar.HOUR with Calendar.HOUR_OF_DAY.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:And when you have to add hours, let me warn you in advance not to confuse Calendar.HOUR with Calendar.HOUR_OF_DAY.


That was my worry, since DAY_OF_MONTH is not at all what I want. Reading the Javadocs, I would think that Calendar.DATE is different than a Calendar.DAY
for exactly @paul's point.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:DAY_OF_MONTH is not at all what I want.


Why not? Isn't DAY_OF_MONTH exactly parallel to MONTH in the context of adding 1 to it?
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Pat Farrell wrote:DAY_OF_MONTH is not at all what I want.

Why not? Isn't DAY_OF_MONTH exactly parallel to MONTH in the context of adding 1 to it?


I don't know. that is why I'm asking the question here.

But a wise man wrote:
"Let me warn you in advance not to confuse Calendar.HOUR with Calendar.HOUR_OF_DAY."

and that would seem to imply that
Calendar.DATE != Calendar.DAY_OF_MONTH

edited: had Calendar.DAY, which doesn't exist.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calendar.DATE = 5.
Calendar.DAY_OF_MONTH = 5.
Calendar.get is a simple array lookup using the given value as the index.
There is no such field as Calendar.DAY.

Hope that helps.

Edit: The add method in GregorianCalendar is smart enough to roll over any overflow in the value you try to add to the indicated field. So if the date is January 30 and you add 2 to the date, you get February 1. Months roll over the year in the same fashion.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaDocs say that Calendar.DAY_OF_MONTH ... is a synonym for Calendar.DATE and vice versa.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:JavaDocs say that Calendar.DAY_OF_MONTH ... is a synonym for Calendar.DATE and vice versa.


Yes, but there is no Calendar.DAY

If it is dangerous to use Calendar.HOUR_OF_DAY instead of Calendar.HOUR,
it sure feels equally dangerous to use DATE when I really want DAY. not only is there no DAY, but there is no discussion in the javadocs of how to simply add one day.

I can trivially write a few test cases, but that doesn't prove that its working correctly, only that my test cases passed.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:If it is dangerous to use Calendar.HOUR_OF_DAY instead of Calendar.HOUR,
it sure feels equally dangerous to use DATE when I really want DAY. not only is there no DAY, but there is no discussion in the javadocs of how to simply add one day.

I think the difference between Calendar.HOUR_OF_DAY and Calendar.HOUR is important if you're doing a get or a set. However they're effectively identical when using add(). The same is true for DAY_OF_MONTH vs DAY_OF_YEAR or DAY_OF_WEEK - they are treated the same as DAY would be, if it existed. This can be seen clearly if you look at the source for GregorianCalendar's add() method - those constants are treated identically there. They all handle what happens when the given field rolls over, carrying a '1' as necessary.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I highly recommend using Joda Time when you're writing a program that needs to work with dates and times. This library has a much better library for dealing with dates and times than Java's standard Date and Calendar classes.

A future version of Java is most likely going to contain a new date and time API which will look a lot like what's currently available in Joda Time (see JSR-310; Stephen Colebourne, Joda Time's author, is leading this JSR).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:
Winston Gutkowski wrote:Yes. Calendar.add(). And furthermore, I believe it takes DST into account.
But the primary storage of a Calendar object is just the elapsed time in milliseconds from a date long ago (1970). So there really is not a concept of DST without applying a Locale to it.

Sure there is. A new Calendar object will use the default Locale unless you tell it to use something like GMT, which doesn't involve DST. Simply adding 24 hours doesn't take that into account.

And as far as which field to use is concerned, the add() documentation is quite specific:
"For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling:
add(Calendar.DAY_OF_MONTH, -5)."


Winston

PS: I second Jesper's post about Joda Time wholeheartedly. It puts the Java Calendar framework to shame. You might also want to read the entire Calendar class documentation, because it contains a lot of stuff about adding, including the possibility that a DST adjustment may not work exactly as you hope; particularly if a 25-hour change is required (don't know; I've never tried it). I suspect it will though; probably just bad wording.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:
Junilu Lacar wrote:JavaDocs say that Calendar.DAY_OF_MONTH ... is a synonym for Calendar.DATE and vice versa.


Yes, but there is no Calendar.DAY

If it is dangerous to use Calendar.HOUR_OF_DAY instead of Calendar.HOUR,
it sure feels equally dangerous to use DATE when I really want DAY. not only is there no DAY, but there is no discussion in the javadocs of how to simply add one day.

I can trivially write a few test cases, but that doesn't prove that its working correctly, only that my test cases passed.


The reason for the cautioning not to confuse HOUR_OF_DAY with HOUR is that one is using a 24 hour clock and the other is using a 12 hour clock. They are two different fields with distinct values. DATE and DAY_OF_MONTH have exactly the same value and point to exactly the same field. Debugging in Eclipse will show you this, since you seem reluctant to take my word for it. Examining the source will show you as well: if you look in the implementation for add you'll see a massive switch statement that switches based on the int field value passed in. Thus whenever something is done according to DAY_OF_MONTH, that is the selfsame logic that will be performed passing DATE. They are the same thing.

This advances the calendar by one day.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!