-1

As part of an opening times page, I want to parse a DayOfWeek range as (Monday to Friday) rather than creating a String for every day of the working week. Is there a way that this can be done so that the default locale automatically changes the range? I also expect TalkBack to say the text in Desired output when it is selected.

Desired output

English: Monday to Friday

English (US): Monday through Friday

French: du lundi au vendredi

Japanese: 月曜日から金曜日まで

Current code

String dayMonday = DayOfWeek.MONDAY.getDisplayName(TextStyle.FULL, Locale.getDefault())
String dayFriday = DayOfWeek.FRIDAY.getDisplayName(TextStyle.FULL, Locale.getDefault())

myTv.text = dayMonday + dayFriday

Current result

MondayFriday
wbk727
  • 8,017
  • 12
  • 61
  • 125
  • 2
    Provide language files which specify things like '%1$s to %2$s' for English, '%1$s through %2$s' for US, 'du %1$s au %2$s' for French, etc. (Using the number allows for any languages which say things like 'Friday from Monday' - I don't know if there are any) and then use `String.format` from there? – BeUndead Sep 13 '19 at 14:28
  • @user2478398 This won't work for all languages as some don't use spaces between words. – wbk727 Sep 13 '19 at 15:18
  • 2
    You need to somehow be aware of how to construct that phrase in every possible language. It should be noted that probably not all languages will use the form ` `. This is **way** out of the scope of a datetime package. It is a full-on internationalization task. – Michael Sep 13 '19 at 15:19
  • @Michael I knew that and explained that to @user2478398 hence maybe just simply creating a `String` resource would be better – wbk727 Sep 13 '19 at 15:22
  • Simple solution: send "Monday through Friday", or whatever, in English to the Google Translate API. Keep a cache of the results. – Michael Sep 13 '19 at 15:23
  • @Michael translation are not 100% accurate for every language + my app is offline-based – wbk727 Sep 13 '19 at 16:10
  • 1
    @MacaronLover There's nothing requiring you to include spaces... You can do `%1$s%2$s` with no spaces. – BeUndead Sep 13 '19 at 16:14
  • @user2478398 and what would the `1` and `2` be replaced with then? – wbk727 Sep 13 '19 at 16:19
  • @MacaronLover Unless you employ a professional translator in every human language, Google Translate is going to be the most accurate thing you can get for free. – Michael Sep 13 '19 at 16:30
  • Language nit-picking, but with some significance: To parse means to convert from a string or text to an object or data structure. I don’t think your question is about parsing? I’m getting confused when your title says something different from your question. – Ole V.V. Sep 14 '19 at 05:11

2 Answers2

0

Horribly belated answer, but to detail the point I was trying to get across in my comment.

Having language files for this which specify the structure of the phrase, as well as another one for the days of the week.

For example, you could have your phrasing files like:

phrasing.properties:

phrasing.dayrange=%1$s to %2$s

phrasing_en_US.properties:

phrasing.dayrange=%1$s through %2$s

phrasing_fr_FR.properties:

phrasing.dayrange=du %1$s au %2$s

Note there is no requirement for spaces before/after the %n$s points. String.format (which will eventually be used, see below) has no such restriction.


And then have your day-name files as:

daynames.properties:

dayname.monday=Monday
dayname.tuesday=Tuesday
...

daynames_fr_FR.properties:

dayname.monday=Lundi
dayname.tuesday=Mardi

And then in your code you do something like:

/**
 * Expects {@code fromDay} and {@code toDay} to be
 * {@code Strings} in the format {@literal "monday"},
 * {@literal "tuesday"}, etc. <emph>in English</emph>.
 */
public String formTheString(final String fromDay,
                            final String toDay) {
    final ResourceBundle phrasingBundle = ResourceBundle.get("phrasing");

    // Get the phrasing format: '%1$s ...' style
    final String phrasing = phrasingBundle.getString("dayrange");

    final ResourceBundle dayNameBundle = ResourceBundle.get("daynames");

    // Get the translated day names
    final String localisedFromDay = dayNameBundle.getString(fromDay);
    final String localisedToDay = dayNameBundle.getString(toDay);

    // Use the 'phrasing' as a format, passing in
    // the translated day names as the parameters
    // to it.
    return String.format(phrasing, localisedFromDay, localisedToDay);
}

Alternatively to the 'dayname' files, you can do what the other answer here suggested.

BeUndead
  • 3,463
  • 2
  • 17
  • 21
-1

This may help you. This gets a calander, sets it to the first day of the week.

Then, creates a formatter that outputs the day name, for a particular Locale - in this case France, but you can change it to what you like.

Finally, it prints out the day name, adds 5 days, then prints out the dayname again.

public static void main(String arg[]) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(cal.DAY_OF_WEEK,cal.getFirstDayOfWeek());

    SimpleDateFormat sdf = new SimpleDateFormat("EEEEEEEEEE", Locale.FRANCE);

    System.out.println(sdf.format(cal.getTime()));
    cal.add(cal.DAY_OF_WEEK, 5);
    System.out.println(sdf.format(cal.getTime()));
}

For this example, I get the output:

lundi
samedi
Tony Weston
  • 317
  • 2
  • 9
  • When the question is already using java.time, the modern Java date and time API, (`DayOfWeek` is from there) **please, please** do not suggest the old `GregorianCalendar` and `SimpleDateFormat`. The former is merely poorly designed, the latter is notoriously troublesome, and both are long outdated. This answer certainly will not help the questioner, it rather risks leading him or her astray. – Ole V.V. Sep 14 '19 at 05:15