除了周末或星期六或星期日之间的两个日期之间,如何获得每一天? [英] How to get every day except weekend or Saturday or Sunday between two dates in java?
问题描述
如果在java中提供开始日期和结束日期,那么所有如何生成日期?
hi all how to generate dates if the start date and end date are provided in java?
如下所示:
例如:如果开始日期是15-04-2012
和结束日期是15-06-2012
有这样我想要日期如下,列表应该省略星期六
和星期天
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
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
Regards Tony
推荐答案
使用以下 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);
}
}
这篇关于除了周末或星期六或星期日之间的两个日期之间,如何获得每一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!