检查日历日期是否是星期日 [英] Check if the Calendar date is a sunday
问题描述
我已经尝试了以下事情:
if(date.DAY_OF_WEEK == date.SUNDAY){
System.out.println(Sunday!);
}
然而似乎不起作用?
当我尝试 System.out.Println date.DAY_OF_WEEK
我得到: 7
有谁知道如何检查当前日历是星期天?
更新更多信息
-
所有date.DAY_OF_WEEK是一个日历对象!
-
我确定将日历对象
date
设置为星期天 firt of all the date.DAY_OF_WEEK is a Calendar object!
i made sure to set the Calendar object
date
to a sunday
我得到7的系统输出是当我尝试运行 date.DAY_OF_MONTH
时返回的系统,即使
第二次更新到ALEX
这是或多或少我的代码
日历startDate = Calendar.getInstance();
startDate.set(2012,12,02);
if(startDate.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY){
System.out.println(true);
} else {
System.out.println(FALSE);
}
日历cal = ...;
if(cal.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY){
System.out.println(Sunday!);
}
Calendar.DAY_OF_WEEK
始终等于 7
无论您正在使用的 Calendar
的实例(查看此链接),它是一个创建的常量使用 Calendar.get()
方法来检索正确的值。
这是调用 Calendar.get(Calendar.DAY_OF_WEEK)
,这将返回一周的真正的一天。此外,您将在 Calendar
类中找到有用的值,如 Calendar.SUNDAY
(以及其他日期和月份)为了让您在代码中更明确,并避免像 JANUARY
等于 0
的错误。
修改
像我说的, Calendar
类包含有用的常量供您使用。没有月份号码 12
他们从 0开始
(见上文),所以 DECEMBER
是Java日期处理中的月号 11
。
Calendar startDate = Calendar.getInstance();
startDate.set(2012,Calendar.DECEMBER,02);
if(startDate.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY){
System.out.println(true);
} else {
System.out.println(FALSE);
}
将打印 true
当然。
I am trying to figure out how to make my program count the number of Sundays in a week.
I have tried the following thing:
if (date.DAY_OF_WEEK == date.SUNDAY) {
System.out.println("Sunday!");
}
Yet it does not seem to work?
When I try to System.out.Println the date.DAY_OF_WEEK
I get: 7
Does anyone know how I can check if the current calendar date is Sunday?
UPDATE FOR MORE INFORMATION
The system out print where i get 7 is what it returns to me when i try to run date.DAY_OF_MONTH
even if the day it set to a sunday
2nd UPDATE TO ALEX
This is more or less my code
Calendar startDate = Calendar.getInstance();
startDate.set(2012, 12, 02);
if (startDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
System.out.println("true");
}else {
System.out.println("FALSE");
}
Calendar cal = ...;
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
System.out.println("Sunday!");
}
Calendar.DAY_OF_WEEK
always equals to 7
no matter what instance of Calendar
you are using (see this link), it is a constant created to be used with the Calendar.get()
method to retrieve the correct value.
It is the call to Calendar.get(Calendar.DAY_OF_WEEK)
that will return the real day of week. Besides, you will find useful values in the Calendar
class like Calendar.SUNDAY
(and the other days and months) in order for you to be more explicit in your code and avoid errors like JANUARY
being equal to 0
.
Edit
Like I said, the Calendar
class does contains useful constants for you to use. There is no month number 12
they start at 0
(see above), so DECEMBER
is month number 11
in the Java Date handling.
Calendar startDate = Calendar.getInstance();
startDate.set(2012, Calendar.DECEMBER, 02);
if (startDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
System.out.println("true");
} else {
System.out.println("FALSE");
}
Will print true
of course.
这篇关于检查日历日期是否是星期日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!