0

More details I want to get rid of specific days of the week.

I mean like: {Sat(1), Sun(2), Mon(3) , Tues(4) , Wed (5),Thurs(6) ,Fri(7)}.

Remove Week Days: {Sat(1), Sun(2), Mon(3) , Tues(4) , Wed (5)}.

So I can get the Count of days without the excluded days from the specific start date to the date of now.

Hint: I must Use the Java Calendar

The problem is specifying the day dynamically

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
Mohamed Gabr
  • 430
  • 5
  • 18
  • 3
    Your question is kind of hard to understand. You want to count the number of Thursdays + Fridays between to dates? – OH GOD SPIDERS Jul 26 '17 at 09:38
  • I want to get the count of days from specific from start date to end date excluding this two days – Mohamed Gabr Jul 26 '17 at 09:39
  • 1
    Possible duplicate of [Calculate number of weekdays between two dates in Java](https://stackoverflow.com/questions/4600034/calculate-number-of-weekdays-between-two-dates-in-java) – Janar Jul 26 '17 at 09:50
  • 1
    if your purpose is just counting number of days between 2 dates and without counting some specific days in week, I think this can help: https://stackoverflow.com/questions/4600034/calculate-number-of-weekdays-between-two-dates-in-java – Antony Dao Jul 26 '17 at 09:51
  • 1
    Why do you have to use `Calendar`? Can't you use `LocalDate` ? – Krzysztof Atłasik Jul 26 '17 at 10:03
  • Why do you want to exclude those 2 days? What are the conditions for removing them? – hamena314 Jul 26 '17 at 10:14
  • I want to exclude a specific number of days as i want to get the number of assigned days and non assigned days – Mohamed Gabr Jul 26 '17 at 11:24
  • So you have a user that says "I don't like mondays, they dont count" (for whatever reason). And the user wants to know how many days are between a certain range, without those mondays? I.e.: "From 03/04/2017 to 05/07/2017 ... how many days are there without mondays"? Is this assumption correct? – hamena314 Jul 26 '17 at 12:09
  • 1
    yes this is exactly what i want – Mohamed Gabr Jul 26 '17 at 12:14

1 Answers1

7

If you can use LocalDate and stream you can apply a functional approach.

LocalDate start = LocalDate.of(2015, 2, 1);
LocalDate end = LocalDate.of(2015, 2, 28);

List<DayOfWeek> includedDays = Arrays.asList(DayOfWeek.THURSDAY, DayOfWeek.FRIDAY);

long daysCount = Stream.iterate(start, date -> date.plusDays(1))
        .limit(ChronoUnit.DAYS.between(start, end))
        .filter(d -> includedDays.contains(d.getDayOfWeek()))
        .count();

If you insist on calendar just use:

GregorianCalendar calendarStart = new GregorianCalendar(2015, 2, 1);
GregorianCalendar calendarEnd = new GregorianCalendar(2015, 2, 28);

List<Integer> includedDays = Arrays.asList(GregorianCalendar.THURSDAY, GregorianCalendar.FRIDAY);
long count = 0;
while(!calendarStart.equals(calendarEnd)) {
    if(includedDays.contains(calendarStart.get(Calendar.DAY_OF_WEEK))) {
        count ++;
    }
    calendarStart.add(Calendar.DATE, 1);
}
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76