tl;dr
LocalDate.now().format(
DateTimeFormatter.ofPattern(
"EEEE dd 'de' MMMM" ,
Locale.forLanguageTag( "es-ES" )
)
)
martes 13 de febrero
java.time
The Answer by Robby Cornelissen is correct, but both it and the Question refer to troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
Specify Locale
to determine the human language used for name of day & month.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEEE dd 'de' MMMM" , Locale.CANADA ) ;
String output = zdt.format( f ) ;
Tuesday 13 de February
For the Canadian-French version:
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEEE dd 'de' MMMM" , Locale.CANADA_FRENCH ) ;
String output = zdt.format( f ) ;
mardi 13 de février
Spanish:
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Locale locale = Locale.forLanguageTag( "es-ES" ) ; // Spanish in Spain.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEEE dd 'de' MMMM" , locale ) ;
String output = zdt.format( f ) ;
martes 13 de febrero
Automatically localize
You can avoid hard-coding such localization. Let java.time automatically localize.
To localize, specify:
FormatStyle
to determine how long or abbreviated should the string be.
Locale
to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH ) ;
String output = zdt.format( f ) ;
mardi 13 février 2018
For Spanish in Spain:
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( z );
Locale locale = Locale.forLanguageTag( "es-ES" ) ; // Spanish in Spain. ;
for ( FormatStyle style : EnumSet.allOf( FormatStyle.class ) )
{
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( style ).withLocale( locale ) ;
String output = zdt.format( f );
System.out.println(style + " | " + output );
}
FULL | martes, 13 de febrero de 2018
LONG | 13 de febrero de 2018
MEDIUM | 13 feb. 2018
SHORT | 13/2/18
By the way, understand that Locale
and time zone have nothing to do with one another, completely orthogonal. The locale is for presentation translation/formatting. The time zone determines the wall-clock used in the content of the date-time value. You can have a moment in Europe/Paris
time zone presented in Japanese language (locale), for example.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.