How to parse the name of the day of the week with different patterns in DayOfWeek?

I need to parse the name of the day of the week in DayOfWeek

. The name of the day of the week can be short ("Mon") or long ("Monday").

I currently came up with a solution like this:

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?

+3


source to share


2 answers


You can use DateTimeFormatterBuilder

with additional templates. They will be checked in the order in which you added them. A DateTimeException

will still be reset if all templates 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 result DayOfWeek

can now be final if you want.




Try to use the same code in 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 );
}

      

At startup.

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

      

+3


source


Do not use try-catch

to fulfill conditions. catch

slow . Conditions are better suited for 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 run a performance test with 100,000 analyzes, the operator if

will be much faster.

0


source







All Articles