Reputation: 3879
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
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
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
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