0

I have this time 20220312T153020 from a json and I want to convert it to: 2022-03-12 15:30

I tried this code, but I'm getting an exception.

val fecha = "20220312T153020".replace("T","")
        val formattedDate = SimpleDateFormat("yyyyMMDDHHmmss").format(fecha)
        Log.d("fecha", formattedDate)

Is possible transform that timestamp to human date? I'm trying kotlin code.

thanks so much.

  • Can you define *human date* – Jens Mar 12 '22 at 16:43
  • I want something like this: 2022-03-12 15:30 But I don't know why the owener of the api rest sends the time of that way – FreddicMatters Mar 12 '22 at 16:44
  • Read this: https://www.baeldung.com/kotlin/dates#:~:text=The%20default%20way%20of%20formatting%20Date%20using%20default,create%20a%20date%20var%20date%20%3D%20LocalDate.parse%20%28%222018-12-31%22%29 – Jens Mar 12 '22 at 16:47
  • I want api level <= 21 that operations requieres api level 26 – FreddicMatters Mar 12 '22 at 16:55
  • But it should be very similar. You have to parse your date first and than you can format it – Jens Mar 12 '22 at 16:55
  • https://stackoverflow.com/a/11046198/3636601 – Jens Mar 12 '22 at 16:57
  • 1
    BTW original time (JSON) is ISO8601 and is considered the most human-readable date time. – YaMiN Mar 14 '22 at 12:22
  • Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [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. Mar 14 '22 at 12:26
  • *But I don't know why the owener of the api rest sends the time of that way* That’s because they adhere to the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) standard. “Human dates” are for humans. Machine-readable dates are for machines. ISO 8601 is a very nice compromise in that it is at the same time pretty human readable and pretty machine readable. – Ole V.V. Mar 14 '22 at 12:28
  • Beware of two things: (1) the case of format pattern letters. Did you intend `DD` or `dd`? (2) The difference between formatting and parsing. They are opposite operations. Did you mean to format a date into a string or parse a string into a date? – Ole V.V. Mar 14 '22 at 12:29
  • As I said, use java.time, the modern Java date and time API. `LocalDateTime.parse(fecha, DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmss")).format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.forLanguageTag("sv")))` gives `2022-03-12 15:30` if you *don’t* remove the `T` first. You should substitute your user’s locale instead of `Locale.forLanguageTag("sv")`, for example `Locale.getDefault()`. And please break up the code into more code lines for readability (in a comment here it’s unreadable anyway, but you can make a nice and readable piece of code out of it). – Ole V.V. Mar 14 '22 at 12:49
  • Does this answer your question? [Java : Cannot format given Object as a Date](https://stackoverflow.com/questions/10649782/java-cannot-format-given-object-as-a-date) – Ole V.V. Mar 15 '22 at 05:28

1 Answers1

1

You should use parse(string) to create date object not format(object).

val fecha = "20220312T153020"

// escaping T by quotes 
val sdf = SimpleDateFormat("yyyyMMDD'T'HHmmss")

// attention to parse not format
val formattedDate = sdf.parse(fecha)

Log.d("fecha", formattedDate)

You can find more information here

https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#parse(java.lang.String)

public Date parse(String source)
           throws ParseException

Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string. 
ocos
  • 1,901
  • 1
  • 10
  • 12