0

I am able to print month in English with these lines of code But how can i Print it in Spanish? I didn't find any Spanish in Locale of (Simple Date Format)

Jazz
  • 73
  • 4
  • Timestamp a= null; (Getting this date from DB); DateFormat date=new Simple Date Format("MMM d, yyyy h:mm a"); String b= date.format(a); – Jazz Dec 05 '19 at 17:21
  • Ene Feb Mar Abr May Jun Jul Ago Sep Oct Nov Dic – Jazz Dec 05 '19 at 17:23
  • 1
    show your code!! – Nikhil S Dec 05 '19 at 17:29
  • I recommend you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Instead use `Month` and/or `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 05 '19 at 17:55
  • Thanks for showing your code. Please add it *in the question* so we have everything in one place, and so that you can format it into a readable format. Code tends to be unreadable when posted in comments. – Ole V.V. Dec 05 '19 at 18:12
  • In the linked original question I recommend the answers [by Basil Bourque]{https://stackoverflow.com/a/40920186/5772882) and [by Mircea Stanciu](https://stackoverflow.com/a/46945783/5772882) using `DateTimeFormatter` from java.time. – Ole V.V. Dec 05 '19 at 18:18

2 Answers2

1

The DateFormat and java.util.Date it uses are outdated. You should (if possible) use the more recent JSR-310 java date/time libraries and formatters that work with it like DateTimeFormatter. Below is an example code with the recent, recommended JSR-310 Java date/time library and the recent DateTimeFormatter using a Spanish (Spain) locale:

LocalDateTime dateTime = LocalDateTime.now();

DateTimeFormatter dateTimeFormatter = DateTimeFormatter
        .ofPattern("MMM d, yyyy h:mm a", new Locale("es", "ES"));

String formattedDateTime = dateTimeFormatter.format(dateTime);

If you for whatever reason have to use the old outdated libraries, then you can use SimpleDateFormat(String pattern, Locale locale). Below is an example code of usage of DateFormat with the Spanish (Spain) locale:

Date dateTime = new Date();

DateFormat date = new SimpleDateFormat(
        "MMM d, yyyy h:mm a", new Locale("es", "ES"));

String formattedDateTime = date.format(dateTime);
Troley
  • 1,421
  • 1
  • 12
  • 14
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Dec 05 '19 at 17:56
  • Yes I know, but the question description states `SimpleDateFormat` and the code snippet in the comment as well, so that's the one I used in the answer. Will update the answer with the `DateTimeFormatter`. Thanks for pointing that out. – Troley Dec 05 '19 at 17:58
1

JDBC 4.2 and java.time

    Locale desiredLanguage = Locale.forLanguageTag("es");
    DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMM", desiredLanguage);

    OffsetDateTime dateTime = yourResultSet.getObject("your_database_column", OffsetDateTime.class);
    String monthString = dateTime.format(monthFormatter);

Or to print in some particular time zone since the month doesn’t change at the same point in time in all time zones:

    ZonedDateTime zdt = dateTime.atZoneSameInstant(ZoneId.of("Europe/Madrid"));
    String monthString = zdt.format(monthFormatter);

Avoid SimpleDateFormat

The SimpleDateFormat class that you referred to is notoriously trouvlesome and long outdated. Don’t use it. java.time, the modern Java date and time API, is so much nicer to work with.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161