• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Gregorian calendar subtract

 
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

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

 
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would use DATE rather than DAY_OF_WEEK.
 
Master Rancher
Posts: 4221
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic