Reputation: 161
I need to make an app that counts how many weekdays it has been. For example is September 23,2014. That's the 17th weekday for the month. Obviously it excludes the weekend. And the counter will start over again when the month is over. So when the date is october 1 then the counter will be equals to 1.
I already have this set of codes it works correctly for the date today which is 11-24-14. The output is 17 which is correct. But for the other dates it's wrong. I haven't put on some validation for example the weekend check because I was testing the logic.
Here's the block of code:
Date dateToday = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(dateToday);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_WEEK);
int week = cal.get(Calendar.WEEK_OF_MONTH);
int dayNo=0;
System.out.println("year= " + year + " month= " + month + " day= "
+ day + " week= " + week);
if ((day == Calendar.SATURDAY) && (day == Calendar.SUNDAY)) {
willPush = false;
}else{
if(week>4){
week-=1;
}
if(week==1){
dayNo+=day-1;
}else if(week==2){
dayNo=day+5;
}else if(week==3){
dayNo=day+10;
}else if(week==4){
dayNo=day+15;
}else if(week==5){
dayNo=day+20;
}
dayNo-=1;
}
System.out.println("DayNO: "+dayNo);
UPDATE: Just figured out that this logic is flawed, very flawed. Will try a new approach, I'm thinking of looping through the days of the month then checking if they are the weekends or not. Not sure if this is the best idea though.
Upvotes: 0
Views: 103
Reputation: 4086
Here is the way I would do this :
public class Main {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.set(2014, 0, 3, 0, 0);
new Main().computeNbDays(c);
}
private void computeNbDays(Calendar cal) {
int day = cal.get(Calendar.DAY_OF_MONTH);
System.out.println("day = " + day);
int nbDays = 0;
for (int i = 1; i <= day; i++) {
cal.set(Calendar.DAY_OF_MONTH, i);
if (workedDay(cal)) {
nbDays++;
}
}
System.out.println(nbDays);
}
private boolean workedDay(Calendar c) {
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
return false;
}
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
return false;
}
return !holidays.contains(c);
}
List<Calendar> holidays = Arrays.asList(
newCalendar(2014, 0, 1), // NEW YEAR
newCalendar(2014, 11, 25) // XMAS
);
private static final Calendar newCalendar(int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day, 0, 0);
return c;
}
}
The purpose is to loop from day 1 to day X, and see if it is a weekday, and not an holiday. You need to customise your holidays list.
Upvotes: 1
Reputation: 372
Here is the sample code. Use joda library.
import org.joda.time.DateTimeConstants;
import org.joda.time.LocalDate;
public class WeekDays {
public static void main(String[] args) {
final LocalDate start = new LocalDate(2014, 1, 1);
final LocalDate end = new LocalDate(2014, 1, 14);
LocalDate weekday = start;
if (start.getDayOfWeek() == DateTimeConstants.SATURDAY ||
start.getDayOfWeek() == DateTimeConstants.SUNDAY) {
weekday = weekday.plusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY);
}
int weekdayCount = 1;
while (weekday.isBefore(end)) {
weekdayCount++;
System.out.println(weekday);
if (weekday.getDayOfWeek() == DateTimeConstants.FRIDAY)
weekday = weekday.plusDays(3);
else
weekday = weekday.plusDays(1);
}
System.out.println(weekdayCount);
}
}
Upvotes: 1