• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Gregorian calendar subtract

 
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • 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.

 
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Tomcat Server Java Linux
  • 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
 
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
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use DATE rather than DAY_OF_WEEK.
 
Master Rancher
Posts: 3719
47
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic