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(
OUTPUT of the above code
Explanation of the code
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);
}
}
‘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
- First a
LocalDate
instance, namedlocalDate
, is created usingLocalDate.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 andyyyy
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, soLocale.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 ofLocale
for Spanish is created using the constructor -new Locale("es", "ES")
, wherees
is value for language(spanish) andES
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 usingLocale.getDefault()
method.
Tutorials on Java 8’s new Date and Time API
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API
Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8
How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions
How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion
Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French
How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enum
Date Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters
Overview of Java 8's new Date and Time APIClick to Read Overview of Java 8's new Date-Time API
Working with time zones in Java 8| ZonedDateTime, ZoneId tutorial with examplesClick to Read tutorial on time zone handling in Java 8
How to convert LocalDate to String and String to LocalDateClick to Read tutorial on String to LocalDate conversions
How to convert java.util.Date to java.time.LocalDateClick to Read tutorial on java.util.Date to LocalDate conversion
Formatting localized dates in Spanish and FrenchClick to Read date formatting in Spanish & French
How to get day-of-week for a given dateHow to get day-of-week using java.time.DayOfWeek enum
Date Modification using TemporalAdjuster Click to Read Tutorial on TemporalAdjusters