How to format a ZonedDateTime to a String?

前端 未结 2 1228
既然无缘
既然无缘 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 18:57

    You can use java.time.format.DateTimeFormatter. https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

    Here there is an example

    ZonedDateTime date = ZonedDateTime.now();
    
    System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date));
    
    0 讨论(0)
  • 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))
    
    0 讨论(0)
提交回复
热议问题