1

I am trying to convert the string to Date like the following:

val inputFormat = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.getDefault())
val s = "Mon, 14 Oct 2019 07:10:28"
val time = inputFormat.parse(s)
Log.d("HttpTools", "time server:$time")

But it show the error

java.text.ParseException: Unparseable date: "Mon, 14 Oct 2019 07:10:28"

Did I missing something ? Thanks in advance.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Wun
  • 6,211
  • 11
  • 56
  • 101
  • 3
    I suggest not to use `Locale.getDefault()`, but `Locale.ENGLISH` because your input to be parsed contains English text. – Meno Hochschild Oct 14 '19 at 14:10
  • Possible duplicate of [Java - Unparseable date](https://stackoverflow.com/questions/6154772/java-unparseable-date) – Ole V.V. Oct 15 '19 at 08:57
  • 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. Oct 21 '19 at 13:11
  • Possibly even more precisely a duplicate of [new SimpleDateFormat(“hh:mm a”, Locale.getDefault()).parse(“04:30 PM”) giving Unparseable exception](https://stackoverflow.com/questions/53313558/new-simpledateformathhmm-a-locale-getdefault-parse0430-pm-giving-un) – Ole V.V. Oct 21 '19 at 13:13
  • Thanks @Meno Hochschild, had a similar problem and for days and your comment solved it in seconds! – Martin S Sep 01 '20 at 21:34

5 Answers5

1

Use this format: EEE, dd MMM yyyy hh:mm:ss

Saurabh Thorat
  • 18,131
  • 5
  • 53
  • 70
  • @Wun It seems to be working for me. Can you show which imports you have used for this? – Saurabh Thorat Oct 14 '19 at 07:49
  • It’s incorrect, introducing a bug. It happens to give the correct result often. but for example for the input string `Wed, 02 Sep 2020 12:58:27` the code in the question correctly gives `Wed Sep 02 12:58:27 CEST 2020` while the code in the answer incorrectly gives `Wed Sep 02 00:58:27 CEST 2020`. – Ole V.V. Sep 02 '20 at 03:00
1

The format is wrong.

According to the documentation, your format should be

EEE, dd MMM yyyy HH:mm:ss

Note that:

  • DDD becomes EEE, as you need the 3 letter day name

  • hh becomes HH, as you need the day hour (0-23). hh will work only if you use am/pm

Robert Estivill
  • 12,369
  • 8
  • 43
  • 64
1

If you are using the java-8 you can use the LocalDateTime and DateTimeFormatter

String text = "Mon, 14 Oct 2019 07:10:28";

DateTimeFormatter format = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss", Locale.getDefault());

LocalDateTime dateTime = LocalDateTime.parse(text, format);

System.out.println(dateTime);    // 2019-10-14T07:10:28
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • A good suggestion in any case. I think the questioner's issue is `Locale.getDefault()` as @Meno Hochschild mentioned in a comment. I suggest using an English-speaking locale instead since the string is in English. – Ole V.V. Oct 17 '19 at 08:48
0

You must use this format to get the day

EEE, dd MMM yyyy HH:mm:ss
Mohit patel
  • 296
  • 1
  • 8
0

Had the same problem and the answer is just as @Meno Hochschild and others suggested in their comments, to not use Locale.getDefault() because you have english text (the name of the month) in your string so if your local from Locale.getDefault() don't return English the parsing fails with exact this exception even if the pattern is right. It's simply because the locale isn't matching the input string language. In my case i had a similar string to yours just the date was different and was using Locale.getDefault() which returned German in my case but the month name in my date string was written in english and so i always received this exception. If you know that the incoming date strings are always localized in english, you can simply set Locale.ENGLISH as the second param of SimpleDateFormat.

Martin S
  • 363
  • 3
  • 14