3

こんにちは、開始日と終了日がJavaで提供されている場合、日付を生成する方法はありますか?

次のように:

例:これがあれば、start date is 15-04-2012日付end date is 15-06-2012を次のようにしたいので、リストは省略saturdayし、sunday

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

私は次のようにしましたが、日付範囲が同じ月の場合、1 か月間生成されます。`

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;
    }

ですから、どうすればよいかアドバイスをください。

よろしくトニー

4

4 に答える 4

4
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);
}
于 2012-06-15T10:29:41.593 に答える
0

次の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);
        }
    }
于 2012-06-15T11:10:42.940 に答える