Convert String to Date in java - Time zone

后端 未结 6 947
情书的邮戳
情书的邮戳 2020-12-21 12:34

I have a String, 2013-10-07T23:59:51.205-07:00, want to convert this to Java date object. I am getting parsing error.

date = new SimpleDateForma         


        
6条回答
  •  渐次进展
    2020-12-21 13:40

    Use this trick to parse ISO8601 datetime format. I admit have not tried this with millisecond part within a string value maybe it gives you an extra headache. This works for Java6.

    import javax.xml.bind.DatatypeConverter;
    Calendar cal = DatatypeConverter.parseDateTime(strDatetime);
    

    If am remembering correct cal instance may not use a system-default timezone. Its initialized to the origin string value timezone. If you want instance to use system timezone you can do this conversion.

       long ts = cal.getTimeInMillis();
       cal = Calendar.getInstance();
       cal.setTimeInMillis(ts);
    

提交回复
热议问题