Joe
Joe

Reputation: 4532

How to get every day except weekend or Saturday or Sunday between two dates in java?

hi all how to generate dates if the start date and end date are provided in java?

like the following :

Ex : if the start date is 15-04-2012 and end date is 15-06-2012 having this i want dates to be like the following and the list should omit saturday and sunday

1.15-04-2012, 2.16-04-2012, 3.17-04-2012, . . . . . . 15-06-2012

I have done like the following but it will generate for one month if the date range is of same month. `

public static ConcurrentHashMap<String, String> getWorkingDaysMap(int year, int month, int day){
        int totalworkingdays=0,noofdays=0;
        String nameofday = "";
        ConcurrentHashMap<String,String> workingDaysMap = new ConcurrentHashMap<String,String>();
        Map<String,String> holyDayMap = new LinkedHashMap<String,String>();
        noofdays = findNoOfDays(year,month,day);

        for (int i = 1; i <= noofdays; i++) {
            Date date = (new GregorianCalendar(year,month - 1, i)).getTime(); // year,month,day
            SimpleDateFormat f = new SimpleDateFormat("EEEE");
            nameofday = f.format(date);

            String daystr="";
            String monthstr="";

            if(i<10)daystr="0";
            if(month<10)monthstr="0";

            String formatedDate = daystr+i+"/"+monthstr+month+"/"+year;

            if(!(nameofday.equals("Saturday") || nameofday.equals("Sunday"))){
                workingDaysMap.put(formatedDate,formatedDate);
                totalworkingdays++;
            }
        }

        return workingDaysMap;
    }

So please do advice me how to go about.

Regards Tony

Upvotes: 3

Views: 4053

Answers (4)

Peter1982
Peter1982

Reputation: 518

I prefer usage of LocalDate.

import java.time.DayOfWeek;
import java.time.LocalDate;

public class Game {
    public static void main(String[] args) {
       LocalDate start = LocalDate.of(2018, 6, 1);
       LocalDate end = LocalDate.of(2018, 8, 1);

       LocalDate d = start;
       while(d.compareTo(end) <= 0) {
         if (d.getDayOfWeek() != DayOfWeek.SATURDAY && d.getDayOfWeek() != DayOfWeek.SUNDAY) {
            System.out.println(d);
         }
         d = d.plusDays(1);
      }
   }
}

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240898

int noOfDaysBetween = 5;
Calendar cal  = Calendar.getInstance();
cal.setTime(startDate);

for(int index = 0 ; index < noOfDaysBetween; index++){
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    if( dayOfWeek!=Calendar.SUNDAY && dayOfWeek!=Calendar.SATURDAY) {
        System.out.println(cal.getTime());
    }
    cal.add(Calendar.DATE, 1);
}

Upvotes: 4

ewan.chalmers
ewan.chalmers

Reputation: 16235

public static void main(String[] args) {

    Calendar calendar = Calendar.getInstance();

    //15-04-2012
    calendar.set(Calendar.DAY_OF_MONTH, 15);
    calendar.set(Calendar.YEAR, 2012);
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MONTH, 3);
    Date start = calendar.getTime();

    //15-06-2012
    calendar.set(Calendar.MONTH, 5);
    Date end = calendar.getTime();

    calendar.setTime(start);
    Date d = null;
    while((d = calendar.getTime()).before(end) || d.equals(end)) {
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        if (day != Calendar.SATURDAY && day != Calendar.SUNDAY) {
            System.out.println(d);
        }
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    } 
}

Upvotes: 1

Joe
Joe

Reputation: 4532

use the following joda-time

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, 06, 15);
        final LocalDate end = new LocalDate(2012, 07, 14);
        LocalDate weekday = start;
        if (start.getDayOfWeek() == DateTimeConstants.SATURDAY|| start.getDayOfWeek() == DateTimeConstants.SUNDAY) {
            weekday = weekday.plusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY);
        }

        while (weekday.isBefore(end)) {
            System.out.println(weekday);
            if (weekday.getDayOfWeek() == DateTimeConstants.FRIDAY)
                weekday = weekday.plusDays(3);
            else
                weekday = weekday.plusDays(1);
        }
    }

Upvotes: 0

Related Questions