• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Date (GregorianCalender)

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all,
I'm working on billing system, where i have to determine if the start date is a weekday. if it isnt i have to make it one day less the till it is a week day. how do i check if it is a week day.
Tys Seyffert
 
Tys Seyffert
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is what i want. it's in VB

' * returns the previous working date from the given date
Private Function getPWDate(CurrentDate As Date) As Date
Dim dRetDate As Date
dRetDate = CurrentDate - 1
Do Until Weekday(dRetDate, vbMonday) < 6
dRetDate = dRetDate - 1
Loop
getPWDate = dRetDate
End Function
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JUnit unit test:

The utility class:

Good luck. You could write a better function to do exactly what you want to do--if day of week is sunday or saturday, just use the add method on calendar to subtract 2 or one respectively from the calendar's date field.
-Jeff L.-
 
Tys Seyffert
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much. I came right though.I did it like this:
private Date getPreviousWorkDay(Date dCurrent){
Date dDate = new Date();
GregorianCalendar g = new GregorianCalendar();
g.setTime(dCurrent);
g.roll(GregorianCalendar.DATE, false);
SimpleDateFormat smd = new SimpleDateFormat("E yyyy.MM.dd");
String sDate = smd.format(dDate);
sDate = sDate.substring(0, 3);
while(sDate.equalsIgnoreCase("Sun") || sDate.equalsIgnoreCase("Sat")){
g.roll(GregorianCalendar.DATE, false);
dDate = g.getTime();
sDate = smd.format(dDate);
sDate = sDate.substring(0, 3);
}
return dDate;
}
 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that works, ok, but it will execute considerably slower. It also does a lot of things it doesn't need to. SimpleDateFormat is a pig of a class. I'd recommend the first solution, which is a simpler 3-line method.
-Jeff-
[ January 20, 2004: Message edited by: Jeff Langr ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Jeff]: I'd recommend the first solution, which is a simpler 3-line method.
Well it would be a little longer if it were to change Saturdays and Sundays to the previous Friday, as originally required. Nonetheless I agree, Calendar is much better suited for this than SimpleDateFormat. Another problem with SDF here it that Tys is relying on locale-specific abbreviations for day of week. Changing locale may well break the code.
By the way, Tys - you may want to check your requirements further - is it enough that the date is a weekday, or do you also need to avoid certain holidays? If it's the latter, you'll need to get a list of the holidays and dates to be avoided. This could get complex since some holidays have variable date. Something to think about...
[ January 20, 2004: Message edited by: Jim Yingst ]
 
Tys Seyffert
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much guys. I'll look into it!
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic