错误的日子crom calendar.getInstance

时间:2016-04-21 07:15:17

标签: java android

今天的日期: 4/21/2016 thursday

Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int weekOfDay = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);

Utilities.trace("MainActivity, year = " + year + " month = " + month + " day = " + weekOfDay);

但结果我得到day = 3。我不明白为什么......

3 个答案:

答案 0 :(得分:2)

您正在打印weekOfDay day

Utilities.trace("MainActivity, year = " + year + " month = " + month + " day = " + weekOfDay);

将其更改为

Utilities.trace("MainActivity, year = " + year + " month = " + month + " day = " + day);

由于WeekOfDay将从0开始,其周四显示为3

答案 1 :(得分:0)

在日历月中从0开始,因此在其中添加1

Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int weekOfDay = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);

并以这种方式显示

Utilities.trace("MainActivity, year = " + year + " month = " + month + " day = " + day);

所以你会得到日期2014 = 4 = 21

答案 2 :(得分:0)

试试这个

    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH)+1;
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int weekOfDay = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);

    Utilities.trace("MainActivity, year = " + year + " month = " + month + " day = " + day);