我有一个要求,我可以提供startDate
(27JUL14
)和endDate
(30JUN15
)和dayOfWeek
({{1 }},4
,5
)
星期几表示 -
6
1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
6 - Saturday
7 - Sunday
和startDate
之间的持续时间为endDate
个月,我需要获得与所提供的6
匹配的所有特定日期{ {1}}和dayOfWeek
。
StartDate
我试图获得周(星期的开始日期)
EndDate
现在我已尝试创建scheduleDates:
public void calculateScheduleDates(Date periodOfOperationFrom, Date periodOfOperationTo, String dow)
{
String[] weekDays = dow.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
int[] dayOfWeek = new int[weekDays.length];
for (int i = 0; i < weekDays.length; i++)
{
try
{
dayOfWeek[i] = Integer.parseInt(weekDays[i]);
dateTime = (DateTime) DateTime.now().withDayOfWeek(dayOfWeek[i]);
System.out.println("DateTime :\t"+dateTime);
}
catch (NumberFormatException nfe) {};
}
}
我需要这个Java解决方案。
我尝试使用public void getWeekDate(String beginDate, String finishDate)
{
System.out.println("Begindate "+ beginDate + "\t EndDate \t"+finishDate);
LocalDate startDate = new LocalDate(2014, 12, 1);
LocalDate endDate = new LocalDate(2015, 6, 30);
/* These are date format for startDate and endDate which are not working with 27JUL14 format.
This also needs to be changed
*/
LocalDate thisMonday = startDate.withDayOfWeek(DateTimeConstants.MONDAY);
if (startDate.isAfter(thisMonday))
{
startDate = thisMonday.plusWeeks(1); // start on next monday
}
else
{
startDate = thisMonday; // start on this monday
}
while (startDate.isBefore(endDate))
{
dates.add(startDate);
startDate = startDate.plusWeeks(1);
}
System.out.println("Size of List Of Date :\t"+dates+"\n"+dates.size());
}
库,但无法获得任何具体结果。
注意:请注意,我需要提到持续时间之间的具体日期,而不仅仅是天数。
请帮助,我非常需要这个。
答案 0 :(得分:0)
一种可能性:
startDate
/ endDate
startDate
行进所需的dayOfWeek endDate
。示例代码:
public class App {
public static void main(final String[] args) {
calculateScheduleDates("27JUL14", "30JUN15", DateTimeConstants.MONDAY);
}
private static void calculateScheduleDates(final String startDate, final String endDate, final int dayOfWeek) {
final DateTimeFormatter formatter = DateTimeFormat.forPattern("ddMMMy").withLocale(Locale.US);
final LocalDate end = LocalDate.parse(endDate, formatter);
LocalDate start = LocalDate.parse(startDate, formatter);
// find the first matching date
while (start.getDayOfWeek() != dayOfWeek) {
start = start.plusDays(1);
}
// compute and print results
while (start.compareTo(end) < 0) {
System.out.println(start.toString(formatter).toUpperCase());
start = start.plusDays(7);
}
}
}
输出:
28JUL14
04AUG14
...
15DEC14
22DEC14
...
22JUN15
29JUN15