How to format a ZonedDateTime to a String?

前端 未结 2 1236
既然无缘
既然无缘 2020-12-29 18:27

I want to convert a ZonedDateTime to a String in the format of ("dd/MM/yyyy - hh:mm"). I know this is possible in Joda-Time

2条回答
  •  半阙折子戏
    2020-12-29 19:03

    Many thanks for above. Here it is in scala where localDateTime.now always Zulu/UTC time.

    import java.time.format.DateTimeFormatter
    import java.time.LocalDateTime
    import java.time.ZoneId
    
    val ny = ZoneId.of("America/New_York")
    val utc = ZoneId.of("UTC")
    val dateTime = LocalDateTime.now.atZone(utc)
    
    val nyTime = DateTimeFormatter.
          ofPattern("yyyy-MMM-dd HH:mm z").
          format(dateTime.withZoneSameInstant(ny))
    

提交回复
热议问题