英文:
convert date in android
问题
我想转换这个日期,但是解析例外,结果应该像这样:"2019-12-28 14:00:00"
try {
String strCurrentDate = "April 08 2020 4:24 AM";
SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy hh:mm a");
Date newDate = format.parse(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(newDate);
Log.d("datessscc", date);
} catch (ParseException e) {
e.printStackTrace();
}
英文:
I want to convert this date but parse exemption it should be like this result "2019-12-28 14:00:00"
try {
String strCurrentDate = "April 08 2020 4:24 AM"
SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy hh:mm a");
Date newDate = format.parse(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(newDate);
Log.d("datessscc", date);
} catch (ParseException e) {
e.printStackTrace();
}
答案1
得分: -1
请更改您的 SimpleDateFormat
试试这样:
SimpleDateFormat input = new SimpleDateFormat("MMM dd yyyy hh:mm a");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date getAbbreviate = input.parse("May 22 2020 4:22 AM"); // 解析输入
Log.i("ouput_Date", "onCreate:" + output.format(getAbbreviate)); // 格式化输出
String date = output.format(getAbbreviate);
} catch (ParseException e) {
e.printStackTrace();
}
英文:
Please change your SimpleDateFormat
Try This:
SimpleDateFormat input = new SimpleDateFormat("MMM dd yyyy hh:mm a");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date getAbbreviate=input.parse("May 22 2020 4:22 AM"); // parse input
Log.i("ouput_Date", "onCreate: "+output.format(getAbbreviate)); // format output
String date=output.format(getAbbreviate);
} catch (ParseException e) {
e.printStackTrace();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论