我最近遇到了一个任务,我必须在一个日期范围内获得所有星期五.我写了一小段代码,很惊讶看到一些奇怪的行为.
以下是我的代码:
public class Friday {
public static void main(String[]args){
String start = "01/01/2009";
String end = "12/09/2013";
String[] startTokens = start.split("/");
String[] endTokens = end.split("/");
Calendar startCal = new GregorianCalendar(Integer.parseInt(startTokens[2]),Integer.parseInt(startTokens[1])-1,Integer.parseInt(startTokens[0]));
Calendar endCal = new GregorianCalendar(Integer.parseInt(endTokens[2]),Integer.parseInt(endTokens[1])-1, Integer.parseInt(endTokens[0]));
int startYear = Integer.parseInt(startTokens[2]);
int endYear = Integer.parseInt(endTokens[2]);
int startWeek = startCal.get(Calendar.WEEK_OF_YEAR);
int endWeek = endCal.get(Calendar.WEEK_OF_YEAR);
Calendar cal = new GregorianCalendar();
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
// cal.setMinimalDaysInFirstWeek(7);
ArrayList<String> main = new ArrayList<String>();
while(startYear <= endYear ){
cal.set(Calendar.YEAR, startYear);
System.out.println(cal.getMinimalDaysInFirstWeek());
if(startYear == endYear){
main.addAll(getFridays(startWeek, endWeek, cal));
}
else{
main.addAll(getFridays(startWeek, 52, cal));
startWeek = 1;
}
startYear =startYear +1;
}
for(String s: main){
System.err.println(s);
}
}
public static ArrayList<String> getFridays(int startWeek, int endWeek, Calendar cal){
ArrayList<String> fridays = new ArrayList<String>();
while(startWeek <= endWeek){
cal.set(Calendar.WEEK_OF_YEAR, startWeek);
fridays.add(cal.getTime().toString());
startWeek = startWeek+1;
}
return fridays;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行代码时,我发现2011年的星期五不见了.经过一些调试和在线浏览后,我认为这Calendar.WEEK_OF_YEAR是特定于语言环境的,我必须使用setMinimalDaysInFirstWeek(7)它来修复它.
所以取消注释上面代码中的相关行.
根据我的理解,现在每年的第一周应该从一周的整周开始.
例如2010年1月1日是星期五.但它不应该出现在结果中,因为我将其配置为处理该周从1月3日开始.但是现在我仍然把1月1日视为星期五
我很困惑.有人可以解释为什么会这样吗?
这些Stackoverflow文章对我有所帮助:
这是一个更简单的方法,使用精彩的http://www.joda.org/joda-time/库:
String start = "01/01/2009";
String end = "12/09/2013";
DateTimeFormatter pattern = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime startDate = pattern.parseDateTime(start);
DateTime endDate = pattern.parseDateTime(end);
List<DateTime> fridays = new ArrayList<>();
while (startDate.isBefore(endDate)){
if ( startDate.getDayOfWeek() == DateTimeConstants.FRIDAY ){
fridays.add(startDate);
}
startDate = startDate.plusDays(1);
}
Run Code Online (Sandbox Code Playgroud)
在这结束时,你将在星期五阵列周五.简单?
或者,如果你想加快速度,一旦你周五结束,你可以从使用天数转为使用周数:
String start = "01/01/2009";
String end = "12/09/2013";
DateTimeFormatter pattern = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime startDate = pattern.parseDateTime(start);
DateTime endDate = pattern.parseDateTime(end);
List<DateTime> fridays = new ArrayList<>();
boolean reachedAFriday = false;
while (startDate.isBefore(endDate)){
if ( startDate.getDayOfWeek() == DateTimeConstants.FRIDAY ){
fridays.add(startDate);
reachedAFriday = true;
}
if ( reachedAFriday ){
startDate = startDate.plusWeeks(1);
} else {
startDate = startDate.plusDays(1);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5736 次 |
| 最近记录: |