我正在尝试找到在Java中实现实用程序功能的最佳方法,以查找开始日期和结束日期之间的选定工作日。我受Java 7的限制,所以我正在寻找Joda-Time库的最佳解决方案。 我已经开发了以下解释的实用程序,但是当只选择一天时,它似乎表现不佳。 谁能建议更好的方法? 预先感谢。
public static List<DateTime> findAllDaysForThePattern(long fromDateTime, long toDateTime, Set<Integer> selectedDaysOfWeek) {
DateTime start=null;
DateTime end=null;
List<DateTime> resultDates = new ArrayList<>();
try {
start = new DateTime(getTimestampUptoDaysPrecision(fromDateTime));
end = new DateTime(getTimestampUptoDaysPrecision(toDateTime));
while (start.isBefore(end)||start.equals(end)) {
if(selectedDaysOfWeek.contains(start.getDayOfWeek())) {
resultDates.add(start);
}
start=start.plusDays(1);
}
}catch(Exception e) {
e.printStackTrace();
}
return resultDates;
}
public static void main(String[] args) {
long start = 1521392661000L;//March 18, 2018 5:04:21 PM
long end = 1524029319000L;//April 18, 2018 5:28:39 AM
Set<Integer> selectedDaysOfWeek = new HashSet<>(1,2);//1-Monday--7-Sunday
selectedDaysOfWeek.add(1);
selectedDaysOfWeek.add(2);
List<DateTime> resultDates = findAllDaysForThePattern(start,end, selectedDaysOfWeek);
for(DateTime date: resultDates) {
System.out.println(date);
}
}