1

I have such string which can be formatted like this (depending on timezone, so e.g):

"2018-10-17T15:33:15 UTC"

"2018-10-17T17:03:00 Europe/Praga

"2018-10-18T12:00:00 America/Kentucky/Monticello"

So those strings contains Zone id at the end

How to parse such strings to datetime?

What I was trying:

val dateString = "2018-10-18T12:00:00 America/Kentucky/Monticello"

    ISODateTimeFormat
    .dateTimeParser()
    .parseDateTime(dateString)

UPDATE:

I've also tried:

val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss zzzz")


val parsed: Date = simpleDateFormat.parse(dateString)

But it cannot be parsed

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
K.Os
  • 5,123
  • 8
  • 40
  • 95
  • I recommend you stay away from the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). `SimpleDateFormat` will give you nothing you cannot have from java.time and also from Joda-Time (from which I think you took `ISODateTimeFormat`). – Ole V.V. Oct 18 '18 at 10:00

1 Answers1

4

You can create an own DateTimeFormatter:

val date1 = "2018-10-17T15:33:15 UTC"
val date2 = "2018-10-17T17:03:00 Europe/Prague"
val date3 = "2018-10-18T12:00:00 America/Kentucky/Monticello"

//with JDK
val formatter = java.time.format.DateTimeFormatterBuilder()
    .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
    .optionalStart()
    .appendLiteral(' ')
    .parseCaseSensitive()
    .appendZoneRegionId()
    .toFormatter();
println(ZonedDateTime.parse(date1, formatter))
println(ZonedDateTime.parse(date2, formatter))
println(ZonedDateTime.parse(date3, formatter))

//With Joda Time
val jodaFormatter = org.joda.time.format.DateTimeFormatterBuilder()
    .appendPattern("yyyy-MM-dd'T'HH:mm:ss ZZZ").toFormatter()
println(jodaFormatter.parseDateTime(date1))
println(jodaFormatter.parseDateTime(date2))
println(jodaFormatter.parseDateTime(date3))

This formatter is only able to parse "Europe/Prague", not "Europe/Praga". You can find all supported zone ids here: https://www.mkyong.com/java8/java-display-all-zoneid-and-its-utc-offset/

Rene
  • 5,730
  • 17
  • 20
  • I cannot use DateTimeFormatter.ISO_LOCAL_DATE_TIME, what is an alternative for this? – K.Os Oct 17 '18 at 20:02
  • Why not? What is your environment? JDK? Libs? – Rene Oct 17 '18 at 20:04
  • I am writing Android app - this requires minimum API level 26 which i cannot apply (my minimum is 21) – K.Os Oct 17 '18 at 20:06
  • Yes I do. I just added an JodaTime example. I'm not an Android developer, so I don't know, if you can use JodaTime. – Rene Oct 17 '18 at 20:30
  • Good answer. From the question I see no reason for the optional part since it’s in all the input strings and you couldn’t parse into a `ZonedDateTime` without it. Also `parseCaseSensitive` is the default, so you need not state it explicitly. – Ole V.V. Oct 18 '18 at 09:54
  • While other Android developers are using Joda-time, there’s no reason to. java.time (including `DateTimeFormatter`, `DateTimeFormatterBuilder` and `ZonedDateTime`) has been backported and the backport adapted specifically for Android in [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). See also [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). I suggest that the best solution lies here. – Ole V.V. Oct 18 '18 at 09:56