无法在Android中使用SimpleDateFormat解析MYSQL时间戳

时间:2018-09-20 12:40:44

标签: java android simpledateformat

我正在使用以下内容来解析字符串。但是我得到了不可解析的日期异常。这是代码

String dateString="2018-09-20T11:44:48.000Z";//MYSQL timestamp from server
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss.S'Z'");
Date convertedDate = new Date();
try {
  convertedDate = dateFormat.parse(dateString); //error here
  textTime.setReferenceTime(convertedDate.getTime());
} catch (ParseException e) {
  e.printStackTrace();
}

1 个答案:

答案 0 :(得分:1)

您应该更正SimpleDateFormat

   String dateString="2018-09-20T11:44:48.000Z";

   SimpleDateFormat inputFormat     = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
   SimpleDateFormat outputFormat    = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss.S'Z'");

    Date   convertedDate    = null;
    String strOP            = null;

    try 
    {
        convertedDate = inputFormat.parse(dateString);
        strOP         = outputFormat.format(convertedDate);
        textTime.setReferenceTime(strOP);
    } 
    catch (ParseException e) 
    {
        e.printStackTrace();
    }