1

I'm trying to format a date and then convert it to a local time , by upon trying it keeps throwing an exception that the data is unparsable , i have attached the code below , if anyone could help , Thank you

  • Code that i'm using
  fun test() : String {
       val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", 
       Locale.US)
        inputFormat.timeZone = TimeZone.getTimeZone("GMT")
        val date = inputFormat.parse(input)
        val outputFormat = SimpleDateFormat("MMM dd, yyyy hh:mma", 
        Locale.US)
        outputFormat.timeZone = TimeZone.getDefault()
        return outputFormat.format(date)
    }
Taki
  • 3,290
  • 1
  • 16
  • 41
  • For starters, you're missing the seconds component in your `SimpleDateFormat` string. – Robby Cornelissen May 18 '22 at 01:49
  • @RobbyCornelissen Thank you , i ve edited the code but still crashing with same error – Taki May 18 '22 at 01:57
  • 4
    Your inputFormat is still invalid. It needs to be `SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")`. – Dan Harms May 18 '22 at 02:16
  • @DanHarms Thank you it worked , how about converting time to local time , is my code correct ? Thank you – Taki May 18 '22 at 02:18
  • If it isn't changing the timezone, you can add `outputFormat setTimeZone(TimeZone.getDefault());` prior to formatting. – Dan Harms May 18 '22 at 02:27
  • @DanHarms I have made a change and time is changing , can you please check one more time ? Thank you – Taki May 18 '22 at 02:28
  • Only small suggestion is replace "GMT" with "UTC" and use a capital H for hour values. Otherwise, I believe it looks correct but I haven't run it. I'll leave that part to you :). – Dan Harms May 18 '22 at 02:30
  • @DanHarms Do you mean UTC because of Locale.US ? – Taki May 18 '22 at 02:31
  • Z == UTC or lack of timezone, whereas GMT is an actual timezone. – Dan Harms May 18 '22 at 02:33
  • Ah Alright so whenever a date contains Z , it refers to UTC , got it Thank you – Taki May 18 '22 at 02:34
  • Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. Use [desugaring](https://developer.android.com/studio/write/java8-support-table) in order to use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). It is so much nicer to work with. – Ole V.V. May 18 '22 at 10:57
  • I cannot reproduce. I hand translated your code to Java and got `Jun 18, 2015 07:30PM`. – Ole V.V. May 18 '22 at 10:59
  • Does this answer your question? [Illegal pattern character 'T' when parsing a date string to java.util.Date](https://stackoverflow.com/questions/2597083/illegal-pattern-character-t-when-parsing-a-date-string-to-java-util-date) – Ole V.V. May 18 '22 at 11:01
  • When I look through the edit history of your question, it seems that you have fixed the error in the code that you originally were asking about. Don’t do that on Stack Overflow. The question is supposed to remain here for future readers to learn from, so they necessarily need to see the bug too. – Ole V.V. May 18 '22 at 14:43
  • @OleV.V. That was not my intention , the other guy who was helping i needed to edit the code so that he can see wether the code is correct or not , that was only my intention . Thank you for understanding . – Taki May 18 '22 at 14:45

0 Answers0