首页 > 解决方案 > 错误将此数据转换为日期(“HH:mm a”),(“dd MM”)和(“yyyy”)。如何将其划分为部分:D

问题描述

错误(java.text.ParseException:无法解析的日期:“2017 年 11 月 2 日 09:44:00:000AM”)

这是我在片段上使用的代码

String tanggal = dataObject.getString("tgl_periksa");
DateFormat readFormat = new SimpleDateFormat("MM dd yyyy HH:mm:ss:SSSa");

DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
     date = readFormat.parse( tanggal );
} catch ( ParseException e ) {
  e.printStackTrace();
}
String formattedDate = "";
if( date != null ) {
   formattedDate = writeFormat.format( date );
}

标签: javaandroid

解决方案


将您的 readFormat 模式更改为它:

DateFormat readFormat = new SimpleDateFormat("MMM dd yyyy HH:mm:ss:SSSa")

推荐阅读