Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date (GregorianCalender)

 
Tys Seyffert
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks very much guys. I'll look into it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic