0

So, i found the java.util.Calendar, and tried to use it for a android project i'm working on.

I do not understand at all how Calendar.DAY_OF_WEEK can return 7, when it's Thursday? And now when it's August Calendar.WEEK_OF_YEAR returns 4, which doesn't make any sense at all!

I have tried GregorianCalendar too, and it gives exactly the same results.

Tried to find any documentation about how they count, but i can't find anything. Seems like there is something very obvious, but which i just cannot find out what it is!

The code i wrote is here:

    // Get if daily or weekly
    boolean daily;
    daily = getPrefs.getBoolean("checkbox_daily", false);
    String day = "0";
    if (daily){
        switch(GregorianCalendar.DAY_OF_WEEK){
            case GregorianCalendar.MONDAY:
                Do_stuff();
                break;
            case GregorianCalendar.TUESDAY:
                Do_stuff();
                break;
            case GregorianCalendar.WEDNESDAY:
                Do_stuff();
                break;
            case GregorianCalendar.THURSDAY:
                Do_stuff();
                break;
            case GregorianCalendar.FRIDAY:
                Do_stuff();
                break;
        }
    }
4

2 回答 2

8

GregorianCalendar.DAY_OF_WEEK是恒定的,

你需要calendarInstance.get(GregorianCalendar.DAY_OF_WEEK);

于 2013-08-22T18:59:04.633 回答
1

来自java.util.Calendar文档:

Calendar 使用两个参数定义特定于语言环境的一周 7 天:一周的第一天和第一周的最少天数(从 1 到 7)。这些数字是在构造日历时从语言环境资源数据中获取的。它们也可以通过设置它们的值的方法明确指定。

在设置或获取 WEEK_OF_MONTH 或 WEEK_OF_YEAR 字段时,Calendar 必须确定月份或年份的第一周作为参考点。一个月或一年的第一周定义为从 getFirstDayOfWeek() 开始并至少包含该月或该年的 getMinimalDaysInFirstWeek() 天的最早 7 天时间段。周数 ..., -1, 0 在第一周之前;第 2 周、第 3 周、... 跟随它。请注意,get() 返回的标准化编号可能不同。例如,一个特定的日历子类可以将一年中第 1 周的前一周指定为上一年的第 n 周。

于 2013-08-22T19:03:29.560 回答