1

I'm using android.text.format.DateFormat to format a date as follows:

String formattedDate = DateFormat.format("EEEE dd de MMMM");

I need something like this (in spanish) "Sunday 10 de June" for example, but the letter 'd' in the word 'de' is interpretted as the day in the format, y tried with this format "EEEE dd \de MMMM" and I get: "Sunday 10 e June", missing the 'd'. How can I achieve this?

Thanks in advance.

Alejandro Casanova
  • 3,633
  • 4
  • 31
  • 46

2 Answers2

3

You need to enclose de in single quotes like this: "EEEE dd 'de' MMMM", as documented in the javadoc for SimpleDateFormat.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
0

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154