如何使用 Java 日历检查今天是否是星期日

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2396290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:59:43  来源:igfitidea点击:

How check if today is sunday with Java Calendar

javadatecalendar

提问by Kamil

I wrote few lines of code which doesn't work correctly. Why? Could sb explain me?

我写了几行不能正常工作的代码。为什么?sb能解释一下吗?

    Calendar date = Calendar.getInstance();

    date.set(2010, 03, 7);

    if(date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
        System.out.println("OK");

采纳答案by Desintegr

To avoid making mistakes, you can use Calendar static values for the month, e.g. :

为避免出错,您可以使用该月的 Calendar 静态值,例如:

date.set(2010, Calendar.MARCH, 7);

回答by T.J. Crowder

Because April7th, 2010 isn't a Sunday. Months start with zero: 0 = January, 1 = February, 2 = March, ...

因为2010 年4 月7 日不是星期日。月份从零开始:0 = 一月,1 = 二月,2 = 三月,...

(Also, side note, you've used octal when specifying the month [03instead of 3]. No biggie until you get to September, whereupon 08is an invalid octal number.)

(另外,旁注,您在指定月份时使用了八进制 [03而不是3]。在您到达九月之前没什么大不了的,因此08是无效的八进制数。)

回答by Pointy

Months count from zero:

月份从零开始计数:

date.set(2010, 2, 7);

Also don't get in the habit of writing numbers with leading zeros. That tells Java (and many other languages) that you want the number interpreted as an octal (base 8) constant, not decimal.

也不要养成用前导零写数字的习惯。这告诉 Java(和许多其他语言)您希望将数字解释为八进制(基数为 8)常量,而不是十进制。

回答by Thilo

Probably because the month is 0-based, so you set April, 7th, which is a Wednesday.

可能是因为月份是从 0 开始的,所以您将 4 月 7 日设置为星期三。

回答by codaddict

The month value is 0-based. Java docs for set method of Calendar class.

月份值是从 0 开始的。Java docs for set method of Calendar class.

Also if you want to check if today(the day the program is run :) ) is Sunday, you need not set anything, because the getInstancemethod returns a Calendar object based on the current time in the default time zone with the default locale:

此外,如果您想检查今天(程序运行的那一天 :) 是否是星期日,您不需要设置任何内容,因为该getInstance方法返回一个基于默认时区中的当前时间和默认语言环境的 Calendar 对象:

Calendar date = Calendar.getInstance();    
//date.set(2010, 03, 7);    
if(date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
    System.out.println("OK");

回答by medopal

Is this for Euler 19?

这是为了欧拉 19 吗?

If so, here is a tip, loop from 1901 to 2000, from months 0 to 11, from days 1-31, then ask:

如果是这样,这里有一个提示,从 1901 年到 2000 年,从 0 月到 11 月,从 1-31 天循环,然后问:

if(date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY  && day==1)
     counter++;

回答by Prabhat Kumar Kashyap

For me this code worked properly, please set the exact date by it millisecond and try like this:-

对我来说,这段代码工作正常,请以毫秒为单位设置确切日期,然后像这样尝试:-

    GregorianCalendar dt = new GregorianCalendar();
    dt.setTimeInMillis(getTimestampDDmmYYYY(date).getTime());
    if((dt.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY| dt.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
        return true;

Thanks, Prabhat Kumar Kashyap

谢谢,Prabhat Kumar Kashyap

回答by lwpro2

cal.DAY_OF_WEEK == cal.SATURDAY || cal.DAY_OF_WEEK == cal.SATURDAY

cal.DAY_OF_WEEK == cal.SATURDAY || cal.DAY_OF_WEEK == cal.SATURDAY

should be good enough.

应该够好了。