• 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
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

How to get the previous day excluding weekends?

 
Greenhorn
Posts: 13
1
IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need to have the previous day date but excluding weekends. I tried some stack overflow but can't find what I need. I managed to get the previous day using this method:



How would I add to exclude weekends? Thank you for the help, I'm a bit stuck here.

I was thinking to check if it's Monday and then ask the date -3 days ago, is that the best way? did I just answered my own question :-)

Thanks!
 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop using Calendar. Use the current dates API; you will probably find the classes have methods to find the last “working day” automatically.
 
Saloon Keeper
Posts: 22112
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you don't, the logic is simply to test the day-of-week in a Calendar and if it's Monday, subtract 3 days from it, else subtract 1 day from it. Assuming, of course that your locale defines "weekend" as Saturday and Sunday.
 
lowercase baba
Posts: 12855
52
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote: if it's Monday, subtract 3 days from it, else subtract 1 day from it.


You may need another condition here if the code can run on Sundays...
 
Tim Holloway
Saloon Keeper
Posts: 22112
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:

Tim Holloway wrote: if it's Monday, subtract 3 days from it, else subtract 1 day from it.


You may need another condition here if the code can run on Sundays...



oops.
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic