Lega
Lega

Reputation: 88

How to parse week day name with different patterns into DayOfWeek?

I need to parse week day name into DayOfWeek. Week day name may be either in short ('Mon') or long ('Monday') format.

Currently I've come up with such solution:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE", locale);
DayOfWeek dayOfWeek;
try {
    dayOfWeek = DayOfWeek.from(dtf.parse(value));
}
catch (DateTimeException e) {
    dtf = DateTimeFormatter.ofPattern("EEEE", locale);
    dayOfWeek = DayOfWeek.from(dtf.parse(value));
}

Is there a shorter solution?

Upvotes: 3

Views: 967

Answers (2)

Amir Raminfar
Amir Raminfar

Reputation: 34149

Don't use try-catch to do conditions. catch is slow. Conditions are better fit with if.

final static DateTimeFormatter shortDTF = DateTimeFormatter.ofPattern("EEE", locale);
final static DateTimeFormatter longDTF = DateTimeFormatter.ofPattern("EEEE", locale);

TemporalAccessor parsed; 
try{ 
 if(value.length() > 3){
   parsed = longDTF.parse(value)
 } else {
   parsed = shortDTF.parse(value)  
 }
 dayOfWeek = DayOfWeek.from(parsed);
} catch(DateTimeException e){
  // throw exception here 
}

If you performance test with 100,000 of parses, the if statement will be much faster.

Upvotes: 0

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22224

You can use DateTimeFormatterBuilder with optional patterns. These will be tried in the order you append them. A DateTimeException will still be thrown if all patterns fail.

final DateTimeFormatter dtf = new DateTimeFormatterBuilder()
        .appendOptional(DateTimeFormatter.ofPattern("EEEE"))
        .appendOptional(DateTimeFormatter.ofPattern("E"))
        .toFormatter(locale);

final DayOfWeek dow1 = DayOfWeek.from(dtf.parse("Mon"));
final DayOfWeek dow2 = DayOfWeek.from(dtf.parse("Monday"));

Note how the resulting DayOfWeek can now be final if you want.


Try that same code across multiple languages (multiple locales).

for ( final Locale locale : new Locale[] { Locale.US , Locale.CANADA_FRENCH , Locale.ITALY , Locale.KOREA } ) {
    final String inputShort = ( DayOfWeek.MONDAY.getDisplayName ( TextStyle.SHORT, locale ) );
    final String inputFull = ( DayOfWeek.MONDAY.getDisplayName ( TextStyle.FULL, locale ) );

    final DateTimeFormatter dtf = new DateTimeFormatterBuilder ( )
            .appendOptional ( DateTimeFormatter.ofPattern ( "EEEE" ) )
            .appendOptional ( DateTimeFormatter.ofPattern ( "E" ) )
            .toFormatter ( locale );

    final DayOfWeek dow1 = DayOfWeek.from ( dtf.parse ( inputShort ) ); 
    final DayOfWeek dow2 = DayOfWeek.from ( dtf.parse ( inputFull ) ); 

    System.out.println ( "" );
    System.out.println ( "Language: " + locale.getDisplayLanguage ( Locale.US ) );
    System.out.println ( "inputShort: " + inputShort + " | dow1: " + dow1 );
    System.out.println ( "inputFull: " + inputFull + " | dow2: " + dow2 );
}

When run.

Language: English
inputShort: Mon | dow1: MONDAY
inputFull: Monday | dow2: MONDAY

Language: French
inputShort: lun. | dow1: MONDAY
inputFull: lundi | dow2: MONDAY

Language: Italian
inputShort: lun | dow1: MONDAY
inputFull: lunedì | dow2: MONDAY

Locale: Korean
inputShort: 월 | dow1: MONDAY
inputFull: 월요일 | dow2: MONDAY

Upvotes: 5

Related Questions