如何计算今年以来星期一的数量?

发布于 2024-12-05 16:59:08 字数 2041 浏览 0 评论 0原文

我正在尝试编写一个简单的程序,自年初以来每个第二个星期一(每个工资周期日)都会添加到列表中,发生的事情有点奇怪,但我以前从未使用过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意犹 2024-12-12 16:59:08

为什么要添加最后日期? 9 月 26 日还没有发生。

因为您在检查日期与当前时间的关系之后提前了日期,并且在将其添加到列表之前

 while ((cal.getTime().compareTo((currentDate.getTime())) < 0)) {
     cal.set(Calendar.WEEK_OF_YEAR, counter);
     dates.add(cal.getTime());
     counter += 2;
 }

这应该可以解决该问题(但可能会引入其他问题):

 while ((cal.getTime().compareTo((currentDate.getTime())) < 0)) {
     dates.add(cal.getTime());
     counter += 2;
     cal.set(Calendar.WEEK_OF_YEAR, counter);
 }

Why is it adding the very last date? Sep 26th hasn't even occurred yet.

Because you advance the date after you check its relation to the current time, and before you add it to the list:

 while ((cal.getTime().compareTo((currentDate.getTime())) < 0)) {
     cal.set(Calendar.WEEK_OF_YEAR, counter);
     dates.add(cal.getTime());
     counter += 2;
 }

This should fix that problem (may introduce others though):

 while ((cal.getTime().compareTo((currentDate.getTime())) < 0)) {
     dates.add(cal.getTime());
     counter += 2;
     cal.set(Calendar.WEEK_OF_YEAR, counter);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文