This is what is my code to print between two dates and it excludes the saturday and sunday but this one does not print the last date of the given month.
import org.joda.time.DateTimeConstants;
import org.joda.time.LocalDate;
public class DatesexcludingWeekend {
public static void main(String[] args) {
final LocalDate start = new LocalDate(2012, 05, 1);
final LocalDate end = new LocalDate(2012, 05, 31);
LocalDate weekday = start;
if (start.getDayOfWeek() == DateTimeConstants.SATURDAY|| start.getDayOfWeek() == DateTimeConstants.SUNDAY) {
weekday = weekday.plusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY);
}
while (weekday.isBefore(end)) {
String dateValues[] = weekday.toString().split("-");
//System.out.println(dateValues[2]+"/"+dateValues[1]+"/"+dateValues[0]);
String date=dateValues[2]+"/"+dateValues[1]+"/"+dateValues[0];
System.out.println("date : "+date);
if (weekday.getDayOfWeek() == DateTimeConstants.FRIDAY)
weekday = weekday.plusDays(3);
else
weekday = weekday.plusDays(1);
}
}
}
Here is the out put of the above code :
date : 01/05/2012
date : 02/05/2012
date : 03/05/2012
date : 04/05/2012
date : 07/05/2012
date : 08/05/2012
date : 09/05/2012
date : 10/05/2012
date : 11/05/2012
date : 14/05/2012
date : 15/05/2012
date : 16/05/2012
date : 17/05/2012
date : 18/05/2012
date : 21/05/2012
date : 22/05/2012
date : 23/05/2012
date : 24/05/2012
date : 25/05/2012
date : 28/05/2012
date : 29/05/2012
date : 30/05/2012
if you see this 31-05/2012 is not getting printed
Please help me to get this solved.
Regards
Tony
It is because you say:
If you want to include the last day then two choices:
Set end to one day after the target date (leave
whilecondition as it was)Change the
whilecondition.An alternative while condition would be something like this: