How to get every day except weekends or Saturdays or Sundays between two dates in java?

Hi everyone, how to create dates if start date and end date are specified in java?

as below:

Ex: if start date is 15-04-2012 and end date is 15-06-2012 having this, I want the dates to be as below, and saturday and sunday should omit the list

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

I did the following, but will be generated within one month if the date range is one 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 help me how to do this.

Relationship tony

+2
source share
3 answers

use 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); } } 
0
source
 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); } 
+4
source
 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); } } 
+1
source

Source: https://habr.com/ru/post/1216043/


All Articles