• Post Reply Bookmark Topic Watch Topic
  • New Topic

Gregorian calendar subtract  RSS feed

 
Mauro Trevigno
Ranch Hand
Posts: 99
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Im trying to subtract 1 day from a date using Calendar but didnt work.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps DAY_OF_WEEK is not the right constant to use? (Hint: it's not.)

And the rest of that code looks rather, er, odd. What else are you trying to do besides subtract?
 
Mauro Trevigno
Ranch Hand
Posts: 99
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Perhaps DAY_OF_WEEK is not the right constant to use? (Hint: it's not.)

And the rest of that code looks rather, er, odd. What else are you trying to do besides subtract?


Which is the right constant to use?

Actually i just realize what was the mistake.



Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mauro Trevigno wrote:
if(Day == 1 || Day == 7)


Don't use those magic numbers. Use the appropriate contsants instead.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use DATE rather than DAY_OF_WEEK.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Perhaps DAY_OF_WEEK is not the right constant to use? (Hint: it's not.)

Hint: yes it is. An add() using DAY_OF_WEEK has exactly the same effect as an add using DATE, DAY_OF_MONTH, or DAY_OF_YEAR. A get(), set(), or roll() will behave differently, but not add() - the date/time gets adjusted the same way for any of those constants I just listed. We may regard DAY_OF_WEEK as less intuitive to us to use here, but the effect is the same. And of course, for the part of the code that depends on get() above, DAY_OF_WEEK is the correct field to use, since it's code that's trying to skip Saturday and Sunday.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!