Java 8 - DateTimeFormatter and ISO_INSTANT issues with ZonedDateTime

后端 未结 4 1314
故里飘歌
故里飘歌 2020-12-13 23:59

So I would expect this code to work under the new Java 8 date/time package since all it does is to convert a given ZonedDateTime to string and back using the same built-in D

4条回答
  •  鱼传尺愫
    2020-12-14 00:09

    If you're using org.threeten.bp from Java 1.8, here's a Cheat Sheet on dealing with String to Date and vice versa.

    Date to String

    val date = Date()
    val calendar = Calendar.getInstance().apply { time = date }
    val zonedDateTime = DateTimeUtils.toZonedDateTime(calendar)
    val formattedDate = DateTimeFormatter.ISO_INSTANT.format(zonedDateTime)
    

    String to Date

    val dateString = "2020-03-04T09:04:43.835Z"
    val dateInstant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(dateString))
    DateTimeUtils.toDate(dateInstant)
    

提交回复
热议问题