0

I've got a number representing the day of the week (directly originating from a constant Calendar.MONDAY for example). Now I'd like to get the user visible text of that day (using default locale of my mobile for the correct translation). I don't have a Calendar instance yet, just the number!

Currently I'm using code below, but I've got the feeling this should be easier, or at least without instantiating a complete Calendar (but I don't find a way to easily set the day_of_week to a Date).

private String LOCALE = Locale.getDefault();
private String FORMAT_DAY_OF_WEEK = "EEEE";

public convertDayOfWeekFromNumberToText(int dayOfWeekNumber) {
  Calendar cal = Calendar.getInstance(LOCALE);
  cal.set(Calendar.DAY_OF_WEEK, dayOfWeekNumber);
  return new SimpleDateFormat(FORMAT_DAY_OF_WEEK, LOCALE).format(cal.getTime());
}

Is this really the easiest way to retrieve the text? And is there a better solution if you'd like to get the best performance?

ps. in case Java7 and Java8 solutions are different, please provide both.

Edit

Thanks to John16384 @ comments I've found a better solution (see answer). I've converted the util-method described above to 2 different methods (1 for short and 1 for long format). Additional solutions would still be appreciated!

P Kuijpers
  • 1,593
  • 15
  • 27

2 Answers2

0

Better solution I've found is this:

DateFormatSymbols.getInstance(LOCALE).getWeekdays()[dayOfWeek]

or depending on the format:

DateFormatSymbols.getInstance(LOCALE).getShortWeekdays()[dayOfWeek]

Not the ideal solution when working with a variable format, but it works for me for now (when using only 2 formats). Any additional advice would still be welcome!

P Kuijpers
  • 1,593
  • 15
  • 27
0

If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.

If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).

The code below works for both. The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.

You can use a DayOfWeek and get the corresponding text in any locale:

int dayOfWeek = 1;
// get day of week from 1 (Monday) to 7 (Sunday)
DayOfWeek dow = DayOfWeek.of(dayOfWeek);
// get value in specified locale
System.out.println(dow.getDisplayName(TextStyle.FULL, Locale.getDefault()));
System.out.println(dow.getDisplayName(TextStyle.SHORT, Locale.getDefault()));
System.out.println(dow.getDisplayName(TextStyle.NARROW, Locale.getDefault()));

Assuming that the default locale is English, the output will be:

Monday
Mon
M

You can change the code above to whatever Locale you need.


Calendar.DAY_OF_WEEK has values from 1 (Sunday) to 7 (Saturday), but the new API's DayOfWeek uses values from 1 (Monday) to 7 (Sunday). So to convert from one to another you need to adjust the values:

int d = calendar.get(Calendar.DAY_OF_WEEK);
// convert values for day of week
int dayOfWeek = (d - 1 == 0) ? 7 : d - 1;
// get day of week from 1 (Monday) to 7 (Sunday)
DayOfWeek dow = DayOfWeek.of(dayOfWeek);