2

I am getting a calendar instance

Calendar.getInstance()

In one mobile it is showing Saturday as weekend while in another it is showing Sunday as weekend The timezone of both devices is same

If i set the calendar instance locale to US or any other country it works perfectly. But i dont want it to be constant. I want it to be regional but consistent on all devices

I want the weekend on both devices same if the time zone is same

ADM
  • 20,406
  • 11
  • 52
  • 83
Abdul
  • 869
  • 6
  • 14
  • 1
    https://stackoverflow.com/questions/16522425/android-calendar-changing-the-start-day-of-week – akshay_shahane May 30 '19 at 06:13
  • Calendar.getInstance().setFirstDayOfWeek(2); – akshay_shahane May 30 '19 at 06:14
  • i dont want the start day to be fixed as in america week start on Monday while in Gulf week start on sunday – Abdul May 30 '19 at 06:23
  • so you want it dynamic ? – akshay_shahane May 30 '19 at 06:31
  • then implement logic based on user's location which you can get from device...or if you have info from login. – akshay_shahane May 30 '19 at 06:33
  • The week starts and ends on different days in different regions. It’s *not* related to time zone. Instead it’s related to *locale*. – Ole V.V. May 30 '19 at 06:47
  • Calendar instance picks the default local by default. and both phones are in same region – Abdul May 30 '19 at 06:50
  • What is the region and language setting of each? It *might* have been that the locale data are different if they run different Android versions, but this is speculation. It also *might* be that the JVM on one device doesn’t pick up the phone’s locale setting, again speculation. – Ole V.V. May 30 '19 at 06:58
  • language for both is English and GMT of both app is GMT +5 – Abdul May 30 '19 at 07:01
  • As an aside consider throwing away the long outmoded and poorly designed `Calendar` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. May 30 '19 at 07:01

3 Answers3

2

You can try something like.

Calendar sundayFirst = Calendar.getInstance();
sundayFirst.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Eugene Babich
  • 1,271
  • 15
  • 25
2

The first (and therefore also the last) day of the week varies by locale (not by time zone nor GMT offset). A locale may define a country or region and/or a language. The language isn’t relevant (at least usually not), but the country is. For example Pakistan:

    WeekFields wf = WeekFields.of(Locale.forLanguageTag("ur-PK"));
    System.out.println("Last day of week is " + wf.getFirstDayOfWeek().plus(6));

Output is:

Last day of week is SATURDAY

Or United Arab Emirates, since you mentioned the Gulf (I assumed the Persian Gulf):

    WeekFields wf = WeekFields.of(Locale.forLanguageTag("ar-AE"));

Last day of week is FRIDAY

To use the default locale, specify Locale.getDefault():

    WeekFields wf = WeekFields.of(Locale.getDefault());

Since my locale is Denmark (Europe), I get Last day of week is SUNDAY now.

I am using the WeekFields class of java.time, the modern Java date and time API. The Calendar class that you are using is poorly designed and long outdated, and the modern API offers a richer functionality.

What happened?

Possible explanations for the behaviour you have observed include:

  • Your two devices have different country settings. I consider this the likely explanation. You have not told us the country setting of each device.
  • On at least one device the JVM does not respect the device locale setting. It is possible to start a JVM with a locale setting that differs from that of the underlying system (not that I know how to do that on Android). It is also possible to change the locale setting of the JVM dynamically through one of the Locale.setDefault methods.
  • Locale data have been changed between the Android versions of your devices, so for the country in question, one device thinks that the day starts on Sunday, on the other device Monday. Locale data are being updated all the time.

For getting the matter a little closer I would start by outputting Locale.getDefault().getDisplayCountry() on each device.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

you can try this

Calendar.getInstance().setFirstDayOfWeek(Calendar.SUNDAY);
barbsan
  • 3,418
  • 11
  • 21
  • 28
akshay_shahane
  • 4,423
  • 2
  • 17
  • 30