2

我的代码从字符串“2018-04-14'T'15:38:14”中获取日历日期,当我调用它时,它当前导致异常setTime。编码

    Calendar cal = Calendar.getInstance();
    // format of date yyyy-MM-dd'T'HH:mm:ss
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd 'T' 
    cal.setTime(sdf.parse("2018-04-14'T'15:38:14"));// all done1 

上面的行导致异常说“不可解析的日期”

4

1 回答 1

1

“T”是一个常数。注意细微的差别。格式用单引号中的 T 定义,但是当您传递值时,它就没有了。

Calendar cal = Calendar.getInstance();
// format of date yyyy-MM-dd'T'HH:mm:ss
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
cal.setTime(sdf.parse("2018-04-14T15:38:14"));// all done1
于 2018-04-15T17:10:18.303 回答