线程“主”中的异常java.lang.IllegalArgumentException:太多模式字母:s

时间:2019-08-08 12:36:27

标签: java-8

如何将String转换为Instant

我得到

  

线程“ main”中的异常java.lang.IllegalArgumentException:太多模式字母:s

public class Demo {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                .withZone(ZoneId.systemDefault());

        Instant instant = Instant.now();
        String output = formatter.format( instant );
        System.out.println(output);


        // Convert String to Instant
        String string = "2018-07-17T09:59:51.312Z";
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("YYYY-MM-DD'T'hh:mm:ss.sssZ", Locale.UK);
        LocalDateTime date = LocalDateTime.parse(string, formatter1);
        System.out.println(date.toInstant(ZoneOffset.UTC));
    }
}

0 个答案:

没有答案