ISO8601时间格式之间的转换

@Test
public void ad() throws ParseException {
    TimeZone tz = TimeZone.getTimeZone("UTC");
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    df.setTimeZone(tz);
    System.out.println(df.format(new Date())); // 2018-08-21T13:37:21.059Z

    // Date -> String
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    String s = sdf.format(new Date());
    System.out.println(s); // 2018-08-21T21:37:21+08:00

    // String -> Date
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    Date date = sdf2.parse(s);
    System.out.println(date); // Tue Aug 21 21:37:21 CST 2018
}

你可能感兴趣的:(JAVASE)