Java 8 – How to get Day of Week, Month in Spanish, French for any date using Locale

This quick code tip shows how to get localized day of the week and month for a date(java.time.LocalDate/LocalDateTime) in French or Spanish using java.util.Locale in Java 8 with examples.

Java 8 code to get date in French or Spanish
package com.javabrahman.java8.time;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class DayOfWeekWithLocale {
  public static void main(String args[]){
    LocalDate localDate=LocalDate.of(2016,01,01);

    //Day of week and month in French
    String dateInFrench=localDate.format(DateTimeFormatter.ofPattern("EEEE, dd MMMM, yyyy",Locale.FRENCH));
    System.out.println("'2016-01-01' in French: "+dateInFrench);

    //Day of week and month in Spanish
    Locale spanishLocale=new Locale("es", "ES");
    String dateInSpanish=localDate.format(DateTimeFormatter.ofPattern("EEEE, dd MMMM, yyyy",spanishLocale));
    System.out.println("'2016-01-01' in Spanish: "+dateInSpanish);

   //English is the default locale for my system on which JVM is running
    String dateInEnglish = localDate.format(DateTimeFormatter.ofPattern("EEEE, dd MMMM, yyyy",Locale.getDefault()));
    System.out.println("'01-Jan-2016' in English(default): "+dateInEnglish);
  }
}
 OUTPUT of the above code
‘2016-01-01’ in French:  vendredi, 01 janvier, 2016
‘2016-01-01’ in Spanish:  viernes, 01 enero, 2016
‘01-Jan-2016’ in English(default):  Friday, 01 January, 2016
Explanation of the code

  • First a LocalDate instance, named localDate, is created using LocalDate.of() method. localDate has the value ‘01-01-2016’.
  • To convert the date into French, i.e. with French names for weekdays and months, LocalDate.format() method is used with 2 parameters.
  • The 1st parameter is a java.time.format.DateTimeFormatter instance with format – "EEEE, dd MMMM, yyyy". EEEE specifies the full name of weekday. MMMM specifies the full name of the month. dd is 2-digit date and yyyy is 4-digit year.
  • The 2nd parameter specifies the locale using a java.util.Locale instance. Since, the date is to be formatted in French, so Locale.FRENCH is used.
  • The dateInFrench is printed and is correctly output as ‘vendredi, 01 janvier, 2016’.
  • Formatting localDate in Spanish requires code similar to French formatting with only one difference. Since, there is no pre-defined constant for Spanish locales, hence an instance of Locale for Spanish is created using the constructor – new Locale("es", "ES"), where es is value for language(spanish) and ES is the value for country(Spain).
  • The dateInSpanish is printed and is correctly output as ‘viernes, 01 enero, 2016’.
  • The code for formatting LocalDateTime in French or Spanish will be the same as we used for LocalDate; including the methods used and values passed.
  • Similar to Spanish and French dates, dates in any language such as Italian, German etc, will also be coded in the same way with just the Locale instance changed to that specific country/language.
  • Lastly, I have formatted localDate using my system’s (and JVM’s) default locale(English) obtained using Locale.getDefault() method.

 

Digiprove sealCopyright © 2014-2016 JavaBrahman.com, all rights reserved.