如何计算今年以来星期一的数量?
我正在尝试编写一个简单的程序,自年初以来每个第二个星期一(每个工资周期日)都会添加到列表中,发生的事情有点奇怪,但我以前从未使用过 Calendar 类,并且我想我可能错误地使用了compareTo,但我读了javadoc,看起来没问题。 打包 javasampleapps;
导入java.util.ArrayList; 导入 java.util.Calendar; 导入 java.util.Date; 导入 java.util.GregorianCalendar; 导入java.util.HashMap; 导入java.util.List; 导入java.util.Map;
/**
*
* @author DavidH
*/
public class AddingDaysToCalendar {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Calendar cal = new GregorianCalendar();
System.out.println(cal.get(Calendar.WEEK_OF_YEAR));
cal.add(Calendar.WEEK_OF_YEAR, 1);
System.out.println(cal.get(Calendar.WEEK_OF_YEAR));
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.WEEK_OF_YEAR, 1 + 1);
Calendar currentDate = new GregorianCalendar();
currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int counter = 2;
List<Date> dates = new ArrayList<Date>();
while ((cal.getTime().compareTo((currentDate.getTime())) < 0)) {
cal.set(Calendar.WEEK_OF_YEAR, counter);
dates.add(cal.getTime());
counter += 2;
}
System.out.println("Listing Dates:");
for (Date d : dates) {
System.out.println(d);
}
}
}
它输出以下内容:
39
40
Listing Dates:
Mon Jan 03 13:37:29 AST 2011
Mon Jan 17 13:37:29 AST 2011
Mon Jan 31 13:37:29 AST 2011
Mon Feb 14 13:37:29 AST 2011
Mon Feb 28 13:37:29 AST 2011
Mon Mar 14 13:37:29 ADT 2011
Mon Mar 28 13:37:29 ADT 2011
Mon Apr 11 13:37:29 ADT 2011
Mon Apr 25 13:37:29 ADT 2011
Mon May 09 13:37:29 ADT 2011
Mon May 23 13:37:29 ADT 2011
Mon Jun 06 13:37:29 ADT 2011
Mon Jun 20 13:37:29 ADT 2011
Mon Jul 04 13:37:29 ADT 2011
Mon Jul 18 13:37:29 ADT 2011
Mon Aug 01 13:37:29 ADT 2011
Mon Aug 15 13:37:29 ADT 2011
Mon Aug 29 13:37:29 ADT 2011
Mon Sep 12 13:37:29 ADT 2011
Mon Sep 26 13:37:29 ADT 2011
为什么要添加最后一个日期? 9月26日甚至还没有发生。
I'm trying to write a simple procedure that will add to a List every second Monday (every pay period day) since the beginning of the year, what is happening is kind of strange but I've never worked with the Calendar class before and I think I may be using compareTo incorrectly but I read the javadoc and it seems okay.
package javasampleapps;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author DavidH
*/
public class AddingDaysToCalendar {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Calendar cal = new GregorianCalendar();
System.out.println(cal.get(Calendar.WEEK_OF_YEAR));
cal.add(Calendar.WEEK_OF_YEAR, 1);
System.out.println(cal.get(Calendar.WEEK_OF_YEAR));
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.WEEK_OF_YEAR, 1 + 1);
Calendar currentDate = new GregorianCalendar();
currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int counter = 2;
List<Date> dates = new ArrayList<Date>();
while ((cal.getTime().compareTo((currentDate.getTime())) < 0)) {
cal.set(Calendar.WEEK_OF_YEAR, counter);
dates.add(cal.getTime());
counter += 2;
}
System.out.println("Listing Dates:");
for (Date d : dates) {
System.out.println(d);
}
}
}
It is outputting this:
39
40
Listing Dates:
Mon Jan 03 13:37:29 AST 2011
Mon Jan 17 13:37:29 AST 2011
Mon Jan 31 13:37:29 AST 2011
Mon Feb 14 13:37:29 AST 2011
Mon Feb 28 13:37:29 AST 2011
Mon Mar 14 13:37:29 ADT 2011
Mon Mar 28 13:37:29 ADT 2011
Mon Apr 11 13:37:29 ADT 2011
Mon Apr 25 13:37:29 ADT 2011
Mon May 09 13:37:29 ADT 2011
Mon May 23 13:37:29 ADT 2011
Mon Jun 06 13:37:29 ADT 2011
Mon Jun 20 13:37:29 ADT 2011
Mon Jul 04 13:37:29 ADT 2011
Mon Jul 18 13:37:29 ADT 2011
Mon Aug 01 13:37:29 ADT 2011
Mon Aug 15 13:37:29 ADT 2011
Mon Aug 29 13:37:29 ADT 2011
Mon Sep 12 13:37:29 ADT 2011
Mon Sep 26 13:37:29 ADT 2011
Why is it adding the very last date? Sep 26th hasn't even occurred yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您在检查日期与当前时间的关系之后提前了日期,并且在将其添加到列表之前:
这应该可以解决该问题(但可能会引入其他问题):
Because you advance the date after you check its relation to the current time, and before you add it to the list:
This should fix that problem (may introduce others though):