public class MyCalendar {
GregorianCalendar greg = new GregorianCalendar();
String[] month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
String[] day = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
public void printMonth(int d, int m, int y) {
GregorianCalendar greg= new GregorianCalendar(greg.get(Calendar.YEAR),
greg.get(Calendar.MONTH), 1);
System.out.println(month[greg.get(Calendar.MONTH)]);
System.out.println(greg.get(Calendar.FRIDAY));
System.out.println(greg.get(Calendar.SATURDAY));
}
我无法弄清楚为什么greg.get(日历._____)给了我意想不到的价值。
YEAR, MONTH, DATE all give me correct values: 2015, 9, 6
FRIDAY returns 279
SATURDAY returns 3
SUNDAY returns 2015
TUESDAY returns 41
有人可以向我解释为什么我会得到这些数字吗?我试图找出该月的所有星期六的日期,并认为greg.get(Calendar.SATURDAY)
可能对此有用。
答案 0 :(得分:2)
因此,Calendar#get
允许您获取Calendar
对象的paritcular字段,例如DAY_OF_WEEK
,然后可以通过{{1}将其映射回Calendar.MONDAY
使得理解返回结果更简单。
因此,要计算一个月中Calendar.SUNDAY
的数量,您需要先找到第一个SATURDAY
(是的,我在设置{{{I}}之后尝试使用SATURDAY
1}},但它给出了不一致的结果)
cal.set(Calendar.DAY_OF_WEEK, Calendar.SATUDAY)
然后直接计算7天,直到我们离开当月
DATE
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1);
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
cal.add(Calendar.DATE, 1);
}