1

I receive the date from API like this:

2020-09-10T20:00:00.000Z

when I convert this date, It shows SEPTEMBER 10, 2020 8:00 p. m.

I need show the month in Spanish, e.g Septiembre or Sep

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Sebastian
  • 27
  • 11
  • 1
    Can you show your code? That would help in answering your question – Trevor Sep 09 '20 at 19:19
  • Does this answer your question? [How to format a date by any locale obtained from language tag with java time?](https://stackoverflow.com/questions/47442317/how-to-format-a-date-by-any-locale-obtained-from-language-tag-with-java-time) [This](https://stackoverflow.com/questions/54228178/how-to-localise-date-format-automatically-in-java-8), [this](https://stackoverflow.com/questions/1661325/simpledateformat-and-locale-based-format-string) or [this](https://stackoverflow.com/questions/54230982/get-locale-specific-date-time-format-in-java)? – Ole V.V. Sep 10 '20 at 03:49
  • You probably also don’t want `8:00 p. m.`. The `20:00` in the string is in UTC (denoted by the trailing `Z`). Don’t you want the time in the user’s time zone instead? – Ole V.V. Sep 10 '20 at 03:51

2 Answers2

3

I recommend you do it with the modern java.time date-time API and the corresponding formatting API (package, java.time.format) instead of with the outdated and error-prone java.util date-time API and SimpleDateFormat. Learn more about the modern date-time API from Trail: Date Time. If your Android API level is still not compliant with Java8, check How to use ThreeTenABP in Android Project and Java 8+ APIs available through desugaring.

Do it as follows using the modern date-time API:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // The given date-time string
        String strDateTime = "2020-09-10T20:00:00.000Z";

        // Parse the given date-time string into OffsetDateTime
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);

        // Define the formatter for output in a custom pattern and in Spanish Locale
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd, uuuu hh:mm a", new Locale("es", "ES"));

        // Print instant using the defined formatter
        String formatted = formatter.format(odt);
        System.out.println(formatted);
    }
}

Output:

septiembre 10, 2020 08:00 p. m.

If you still want to use the legacy date-time and formatting API, you can do it as follows:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        // The given date-time string
        String strDateTime = "2020-09-10T20:00:00.000Z";

        // Define the formatter to parse the input string
        SimpleDateFormat inputFormatter = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        // Parse the given date-time string into java.util.Date
        Date date = inputFormatter.parse(strDateTime);
        
        // Define the formatter for output in a custom pattern and in Spanish Locale
        SimpleDateFormat outputFormatter = new SimpleDateFormat("MMMM dd, yyyy hh:mm a", new Locale("es", "ES"));

        // Print instant using the defined formatter
        String formatted = outputFormatter.format(date);
        System.out.println(formatted);
    }
}

Output:

septiembre 10, 2020 08:00 p. m.
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
2

You can try something like this (it returns date in format: 10 de septiembre de 2020 20:00):

val format: DateFormat = DateFormat.getDateTimeInstance(
    DateFormat.LONG, // date format
    DateFormat.SHORT, // time format
    Locale("es", "ES") // Spanish Locale
)

val dateTime = "2020-09-10T20:00:00.000Z"
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale("es", "ES"))
val date: Date = simpleDateFormat.parse(dateTime)!! // without validation

println(format.format(date)) // it prints `10 de septiembre de 2020 20:00`
iknow
  • 8,358
  • 12
  • 41
  • 68
  • 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`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Sep 10 '20 at 03:41