如何在Java中的两个日期之间的除周末,周六或周日之外的每一天获取信息?

大家好,如果在Java中提供了开始日期和结束日期,那么如何生成日期呢?

像下面这样:

例:如果有start date is 15-04-2012并且end date is

15-06-2012我希望日期与以下类似,则列表应省略saturdaysunday

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

15-06-2012

我已经完成了以下操作,但是如果日期范围是同一个月,它将生成一个月。`

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;

}

因此,请为我提供建议。

问候托尼

回答:

使用以下乔达时间

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

}

}

以上是 如何在Java中的两个日期之间的除周末,周六或周日之外的每一天获取信息? 的全部内容, 来源链接: utcz.com/qa/421478.html

回到顶部