在 android 中的 json 中,我收到的日期字符串为“2018-03-13 19:00:00”。 我想转换为“12-03-2018 07:00 PM”。
所以我实现了几行代码。
public static String getCustomDateScoreboard(String dateTimeStr) {
Date date = null;
String formattedTime = "";
try {
date = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss", Locale.US).parse(dateTimeStr);
Log.d(TAG,date.toString());
formattedTime = new SimpleDateFormat("dd-mm-yyyy hh:mm aa", Locale.US).format(date);
} catch (ParseException e) {
Timber.e(e, "date time not in proper format: %s", dateTimeStr);
}
return formattedTime;
}
但它返回的月份为 00,即 13-00-2018 07:00 PM
最佳答案
小号mm
用于表示分钟,使用大写MM
表示月份,如下
public static String getCustomDateScoreboard(String dateTimeStr) {
Date date = null;
String formattedTime = "";
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).parse(dateTimeStr);
Log.d(TAG,date.toString());
formattedTime = new SimpleDateFormat("dd-MM-yyyy hh:mm aa", Locale.US).format(date);
} catch (ParseException e) {
Log.e(e, "date time not in proper format: %s", dateTimeStr);
}
return formattedTime;
}
关于java - Android 日期格式转换为自定义日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49252971/