Question

I have a boolean array, which goes from 1-7 corresponding to week days, starting on monday.

Now, I want to retrieve today's day of week (i.e.: Monday, Tuesday...), starting from Monday. I've been using this code :

Calendar c = Calendar.getInstance();
int index = c.get(Calendar.DAY_OF_WEEK)-1;//array starts on 0 and DAY_OF_WEEK on 1

but since a Calendar.getInstance() is localized, Iwas thinking that depending on the user's location, a Monday could either correspond to 1 or to 0. Now, what I'd like to know is whether or not, if the user's location changes, the DAY_OF_WEEK will change or not?

Could you help me figure it out?

Thanks.

Was it helpful?

Solution

Calendar.get(DAY_OF_WEEK) always returns one of CALENDAR.SUNDAY, Calendar.MONDAY, ..., Calendar.SATURDAY, whatever the locale is. And these are constants which are respectively 1, 2, ... 7.

So if your boolean array starts at Monday (index 0) and ends at Sunday (index 6) you just need this:

int calendarDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int myDayOfWeek = calendarDayOfWeek - 2;
if (myDayOfWeek < 0) {
    myDayOfWeek += 7;
}

OTHER TIPS

Well if you want to get today's date then you should take String :

ActionBar actionBar = getActionBar();
String dateString = (String) android.text.format.DateFormat.format("yyyy/MM/dd", new java.util.Date());
actionBar.setTitle(dateString); 

If you want you can delete first line, i made this with my actionbar.

I hope it helped..

You can also find answer in here :

How do you format date and time in Android?

to get today's day of week..

try like this

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date();
String dayOfTheWeek = sdf.format(d);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top