1

I'm trying to parse a String that represents a Date "2017-05-22" into a Date() using SimpleDateFormat(). I also need to have the date conversion in Spanish, so I'm creating a Locale with the spanish configuration. The problem is that for some reason I still get the output in English. My phones Language & Input configuration by default is English, but I've tried to change it to Spanish as well. Here is the full code:

    val localeSpanish = Locale("es", "ES")
    val dateFormat = SimpleDateFormat("yyyy-MM-dd", localeSpanish)
    val dateInSpanish = dateFormat.parse("2017-01-29")

And I get this in the ouput:

Wed Jan 11 00:00:00 GMT-04:00 2017

Can someone tell me what I'm doing wrong?

Jose Bernhardt
  • 887
  • 1
  • 10
  • 24
  • @mTak missed an e, let me edit. – Jose Bernhardt Jul 27 '18 at 15:17
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jul 27 '18 at 21:47
  • Possible duplicate of [SimpleDateFormatter.parse giving output in different format than specified](https://stackoverflow.com/questions/50494859/simpledateformatter-parse-giving-output-in-different-format-than-specified) – Ole V.V. Jul 27 '18 at 21:51

5 Answers5

1

As stated by others, when you are outputting dateInSpanish, you are exposing a Date instance, which is calling its toString method and its implementation constructs that string based on a static array containing the words in English

//From java.util.Date
private final static String wtb[] = {
    "am", "pm",
    "monday", "tuesday", "wednesday", "thursday", "friday",
    "saturday", "sunday",
    "january", "february", "march", "april", "may", "june",
    "july", "august", "september", "october", "november", "december",
    "gmt", "ut", "utc", "est", "edt", "cst", "cdt",
    "mst", "mdt", "pst", "pdt"
};

When you are declaring the dateFormatter, it is meant to convert a date/string as the following example:

val dateFormatter = SimpleDateFormat("yyyy-MM-dd", localeSpanish)
println(dateFormatter.parse("2017-01-29")) // prints: Wed Jan 29 00:00:00 GMT-02:00 2017
println(dateFormatter.format(Date()) // prints: 2018-07-27 (as today :p)

I think you should use a different mask in order to obtain the formatted string. But if you are forced to read the date in that format, you would have to declare two formatters:

val readerFormatter = SimpleDateFormat("yyyy-MM-dd", localeSpanish)
val writerFormatter = SimpleDateFormat("d 'de' MMMM 'del' yyyy", localeSpanish)
val readDate: Date = readerFormatter.parse("2017-01-29")
val dateInSpanish: String = writerFormatter.format(readDate)
println(dateInSpanish) // prints: 29 de enero del 2017 (as today :p)
crgarridos
  • 8,758
  • 3
  • 49
  • 61
1

Sorry I cannot write Kotlin (yet). Can you translate form Java?

    Locale localeSpanish = new Locale("es", "ES");
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
            .withLocale(localeSpanish);
    LocalDate date = LocalDate.parse("2017-01-29");
    String formattedDate = date.format(dateFormatter);
    System.out.println("Fecha en español: " + formattedDate);

This prints:

Fecha en español: 29 de enero de 2017

Messages:

  • The date-time classes you were using, SimpleDateFormat and Date, are long outdated and poorly designed. SimpleDateFormat in particular has a reputation for being troublesome. And despite the name a Date represents a point in time, not a date. Instead use java.time, the modern Java date and time API.
  • For a date without time of day use the LocalDate class.
  • The format you are parsing, yyyy-MM-dd, is ISO 8601. The modern classes parse ISO 8601 as their default, that is, without any explicit formatter.
  • No matter if you use Date or LocalDate, they haven’t got neither a format nor a locale in them. They just hold the data in much the same way as an int holds a number without any format or locale. No matter if you have an int, a Date or a LocalDate, if you want a specific format, you can have that format only in a String.
  • To format a date for an audience in a locale, use a built-in date format. DateTimeFormatter.ofLocalizedDate gives you one, but you need to convert it to the desired locale.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

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

Simple, SimpleDateFormat.parse returns a Date object which is format agnostic. What you want to do is print the following:

val dateString = dateFormat.format(dateInSpanish)
TheHebrewHammer
  • 3,018
  • 3
  • 28
  • 45
0

Try using this for date representation and for local spanish time use your localSpanish val

 Calendar c = Calendar.getInstance();
 SimpleDateFormat df = new SimpleDateFormat("yyyy MMM dd - HH:mm:ss",*your code here*);
 String formattedDate = df.format(c.getTime());
BigBoiVladica
  • 181
  • 1
  • 12
0

The line:
val dateInSpanish = dateFormat.parse("2017-01-29")
does not format the date, it just extracts the date out of the string
The line:

val localeSpanish = Locale("es", "ES")

does not change your phone's configuration
so you have a date: dateInSpanish and you can format it as you like and then print it