-3

I have value like following "Wed Apr 10 03:48:00 PDT 2019" and want to convert or parse in 2019-04-10 03:48:00.

My excepted result is:

2019-04-10 03:53:02
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76

1 Answers1

2

You should just use DateTimeFormatter:

val dateString = "Wed Apr 10 03:48:00 PDT 2019"

val dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH)

val parsed = ZonedDateTime.parse(dateString , dtf)

val formatted = DateTimeFormatter.ofPattern("yyyy-mm-dd HH:mm:ss").format(parsed)

Remember to pass explicitly Locale as the second argument to DateTimeFormatter.ofPattern because this code might fail on parsing Apr part on a computer with different default locale!

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76