简体   繁体   中英

Getting wrong days crom calendar.getInstance

Today's date: 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);

But as a result I get day = 3 . I don't understand why..

You are printing weekOfDay not day

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

Change it to

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

As WeekOfDay will start from 0, its showing 3 for Thursday

In calendar month starts from 0 so add 1 in it

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);

and show it like this way

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

So you will get the date 2014=4=21

Try this

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM