yubaraj poudel
yubaraj poudel

Reputation: 3879

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..

Upvotes: 0

Views: 41

Answers (3)

Anirudh Loya
Anirudh Loya

Reputation: 173

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

Upvotes: 0

Bajirao Shinde
Bajirao Shinde

Reputation: 1354

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

Upvotes: 0

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

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

Upvotes: 2

Related Questions