1

I am running the below code on 6/7/2018 in order to omit weekends from any dates returned. However the code seems to determine the below days as the weekend.

13/7/2018 - Friday & 14/7/2018 - Saturday

rather than

14/7/2018 - Saturday & 15/7/2018 - Sunday

I am updating the field indicated to increase / reduce the amount of days in the future I want to select.

If I input 5 days the date returned is 12/7/2018 and if I input 6 days the date returned is 15/7/2018.

Is there something obvious I am missing, any help would be much appreciated.

Date date=new Date();
Calendar calendar = Calendar.getInstance();
date=calendar.getTime(); 
SimpleDateFormat s;
s=new SimpleDateFormat("dd/MM/yyyy");

System.out.println(s.format(date));

int days = 5; //I am updating this value to increase and decrease days
for(int i=0;i<days;)
{
    calendar.add(Calendar.DAY_OF_MONTH, 1);

    //here even sat and sun are added
    //but at the end it goes to the correct week day.
    //because i is only increased if it is week day

    if(calendar.get(Calendar.DAY_OF_WEEK)<=5)
    {
        i++;
    }

}
date=calendar.getTime(); 
s=new SimpleDateFormat("dd/MM/yyyy");
System.out.println(s.format(date));
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
SteveL
  • 31
  • 3
  • 1
    I have been looking all over for the answer and currently cannot find what I'm looking for otherwise wouldn't have posted on here. New to this and not worked with dates before so if you could point me in the right direction to the documentation that would be great thanks. Tried lots of different ways of updating the code based on the things I've ben reading but sadly not getting the desired results. – SteveL Jul 06 '18 at 14:04
  • 1
    Why the downvotes, please? I find this question clear and useful. While the research effort may not be very clear to see, I believe it’s been made. The question is very clear about desired behaviour and how observed behaviour differs, and it presents an example that is about minimal and certainly both complete and verifiable. Most first Stack Overflow questions aren’t so good as this one. – Ole V.V. Jul 08 '18 at 06:15

1 Answers1

4
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
            .withLocale(Locale.UK);
    LocalDate date = LocalDate.now(ZoneId.of("Pacific/Truk"));
    System.out.println(date.format(dateFormatter));
    int days = 5;

    int i = 0;
    while (i < days) {
        date = date.plusDays(1);
        DayOfWeek day = date.getDayOfWeek();
        if (! day.equals(DayOfWeek.SATURDAY) && ! day.equals(DayOfWeek.SUNDAY)) {
            i++;
        }
    }
    System.out.println(date.format(dateFormatter));

Output today (Sunday 8th July):

08/07/2018
13/07/2018

13th July is next Friday, so obviously it didn’t take Friday as weekend.

Is there something obvious I am missing(?)

It don’t think it’s that obvious: The Calendar class numbers the days of the week from 1 for Sunday through 7 for Saturday. This comes from an American understanding of weeks. So when your condition was that the day of week should be less than or equal to 5, you included Sunday (1) through Thursday (5) and filtered out Friday (6) and Saturday.

…if you could point me in the right direction to the documentation…

To find this information in the documentation you would have to look under each constant for day of week, SUNDAY, etc., and there follow the link Constant Field Values. See the links at the bottom of this answer.

The Calendar class has proved poorly designed (despite attempts to fix the problems with Date) and is now long outdated too. Instead I recommend that you use java.time, the modern Java date and time API. Which I of course do in the snippet above.

One of many problems with Calendar is the use of int for day of week (and other items that have names rather than being numbers). It’s unnatural and very easy to confuse. One may say that you reinforced the problem by comparing to 5 rather than to Calendar.FRIDAY, but because of the American numbering the latter wouldn’t have solved your issue either. java.time’s DayOfWeek is an enum and doesn’t invite for comparing using “less than” or “is before” (though you may, and it would work in your case). The code referring to named constants SATURDAY and SUNDAY is not only clearer to read, it is also less error-prone.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161