Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Date (GregorianCalender) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date (GregorianCalender)" Watch "Date (GregorianCalender)" New topic
Author

Date (GregorianCalender)

Tys Seyffert
Ranch Hand

Joined: Jan 12, 2004
Posts: 32
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


Sun Certified Java Programmer<br />(SCJP)
Tys Seyffert
Ranch Hand

Joined: Jan 12, 2004
Posts: 32
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

Joined: May 14, 2003
Posts: 762
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.-


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Tys Seyffert
Ranch Hand

Joined: Jan 12, 2004
Posts: 32
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

Joined: May 14, 2003
Posts: 762
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

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
Tys Seyffert
Ranch Hand

Joined: Jan 12, 2004
Posts: 32
Thanks very much guys. I'll look into it!
 
Consider Paul's rocket mass heater.
 
subject: Date (GregorianCalender)