1

Qt/Qlocale has a weekdays() method which gives you the week-days for a locale.

Android only seems to have the Calendar.getFirstDayOfWeek() method which doesn't help - en_GB and en_US have Monday and Sunday as the first day of the week in calendars respectively, but both locales define Saturday/Sunday as the weekend.

A similar question was asked, but it was put on hold: Does Android provide an API for determining weekend days that is locale-aware?

njzk2
  • 38,969
  • 7
  • 69
  • 107

5 Answers5

3

You need to use ICU4J library http://site.icu-project.org and call getWeekData() on the calendar -> http://icu-project.org/apiref/icu4j/com/ibm/icu/util/Calendar.html#getWeekData()

This library supports Persian(Jalali) and Arabic(Hijri, Hijri-civil) calendars and many others.

Calendar.getInstance(new ULocale("fa_IR@calendar=persian"))
Calendar.getInstance(new ULocale("ar_IQ@calendar=islamic-civil"))
Calendar c = Calendar.getInstance(new ULocale("en_GB@calendar=gregorian"))

c.getFirstDayOfWeek()
c.getWeekData().weekendOnset // weekend start
c.getWeekData().weekendCease // weekend end
box
  • 4,450
  • 2
  • 38
  • 38
0

Won't this do it:

Date d = new Date();
int dayOfWeek = d.get(Calendar.DAY_OF_WEEK);
Community
  • 1
  • 1
Buddy
  • 10,874
  • 5
  • 41
  • 58
  • 1
    the OP is asking about how to know which days of the week are weekend days. This answer does not help. – njzk2 Sep 09 '15 at 00:01
  • But he states that: "both locales define Saturday/Sunday as the weekend" and this lets him determine if the day is a Saturday/Sunday – Buddy Sep 09 '15 at 00:32
  • good point. I was refering to the other question, which mentions weekend day in Iran. – njzk2 Sep 09 '15 at 00:40
  • 1
    I should have emphasized that this should work across locales - where the weekend may be completely different (Friday/Saturday). – Elsebeth Flarup Sep 09 '15 at 22:02
0

A smaller alternative to ICU4J is using my library Time4A. Example for localized weekend queries:

Locale yemen = new Locale("ar", "YE");
Weekmodel model = Weekmodel.of(yemen);
System.out.println(
    "Weekend: " + model.getStartOfWeekend() + " - " + model.getEndOfWeekend()
); // Weekend: THURSDAY - FRIDAY

PlainDate today = SystemClock.inLocalView().today();
System.out.println(today.isWeekend(yemen));

The source of the underlying data are based on CLDR (same as in ICU4J).

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
0

You question seems to point to this other thread already. Please take a look at this answer. Note: The API does not give you the weekdays, but enables you to know if it is a weekday / holiday etc at the time of an API call at device location.

If that post answers your question fully, please consider marking your question a duplicate.

MRC
  • 56
  • 4
-1

Once you have the day of the week, you can always determine if its a weekday/weekend using a simple switch or HashMap ?

SimpleDateFormat sdf = new SimpleDateFormat("EEE");
Date d = new Date();
String dayOfTheWeek = sdf.format(d);
switch(dayOfTheWeek){
    case "Mon":
    case "Tue":
    case "Wed":
    case "Thu":
    case "Fri":
        System.out.println("Weekday");
        break;

    default:
        System.out.println("Weekend");
        break;
}
Actiwitty
  • 1,232
  • 2
  • 12
  • 20
  • 2
    But what if my active locale is an Arabic or Hebrew one, with a different definition of weekends? I would want this to work across all locales, obviously. – Elsebeth Flarup Sep 09 '15 at 21:59