0

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.

MagicMicky
  • 3,819
  • 2
  • 37
  • 53
  • I think you are correct. I would suggest set TimeZone, that way you may get correct week (I haven't tested this approach, that is way added as comment. I may be wrong too.) – kosa Jun 02 '13 at 13:10
  • Locale usersLocale = Locale.getDefault();Calendar cal = Calendar.getInstance(usersLocale); int index = cal.get(Calendar.DAY_OF_WEEK)-1; use this – Raghunandan Jun 02 '13 at 13:13
  • @Raghunandan But if I put a specific locale wouldn't it be a problem with the current day (like in the US we could be on Sunday, while in the UK we could be on Monday)? – MagicMicky Jun 02 '13 at 13:14
  • @MagicMicky it would be specific for that location right? – Raghunandan Jun 02 '13 at 13:15
  • @Raghunandan Yeah but I just want to retrieve the day of the week in the user's town, but starting on monday (because my data starts on monday) – MagicMicky Jun 02 '13 at 13:16
  • You should post a [SSCCE](http://sscce.org) that demonstrates the problem you are having with different locales. I don't understand your question, and think you are confused by `Calendar.DAY_OF_WEEK`. You say "_I just want to retrieve the day of the week in the user's town, but starting on monday_" which makes no sense to me. If I were your user, right now, today would be Sunday for me. What has this to do with _starting monday_? – jlordo Jun 02 '13 at 13:18
  • @MagicMicky check this http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html – Raghunandan Jun 02 '13 at 13:18
  • Because I want the DAY_OF_WEEK, which is a number between 1-7, and my problem is that I thought it would be localized depending on the User's actual starting day of week (on the US the week starts on Sunday, so Sunday would be 1, but in France the week starts on Monday which I would guess would be 1). But according to the answer below, the DAY_OF_WEEK isn't localized, so there should be no problems. – MagicMicky Jun 02 '13 at 13:22

3 Answers3

3

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;
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

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?

Community
  • 1
  • 1
AndroidFreak
  • 866
  • 1
  • 10
  • 31
  • I only want the DAY_OF_WEEK (from 1 - 7), not the actual date. And my problem is that I don't want it to depend on the User's locale, but on mine (because of my data) – MagicMicky Jun 02 '13 at 13:15
0

to get today's day of week..

try like this

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date();
String dayOfTheWeek = sdf.format(d);
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
  • The problem would still be the same: the dayOfWeek would be localized, wouldn't it ? (i.e. if the user is in the US, we would have 1 for Sunday, and if the user is in France, we would have 1 for Monday) – MagicMicky Jun 02 '13 at 13:17