How to parse/format dates with LocalDateTime? (Java 8)

后端 未结 7 1224
既然无缘
既然无缘 2020-11-22 01:11

Java 8 added a new java.time API for working with dates and times (JSR 310).

I have date and time as string (e.g. \"2014-04-08 12:30\"). How can I obtai

7条回答
  •  悲&欢浪女
    2020-11-22 02:03

    GET CURRENT UTC TIME IN REQUIRED FORMAT

    // Current UTC time
            OffsetDateTime utc = OffsetDateTime.now(ZoneOffset.UTC);
    
            // GET LocalDateTime 
            LocalDateTime localDateTime = utc.toLocalDateTime();
            System.out.println("*************" + localDateTime);
    
            // formated UTC time
            DateTimeFormatter dTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            System.out.println(" formats as " + dTF.format(localDateTime));
    
            //GET UTC time for current date
            Date now= new Date();
            LocalDateTime utcDateTimeForCurrentDateTime = Instant.ofEpochMilli(now.getTime()).atZone(ZoneId.of("UTC")).toLocalDateTime();
            DateTimeFormatter dTF2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            System.out.println(" formats as " + dTF2.format(utcDateTimeForCurrentDateTime));
    

提交回复
热议问题