将日期时间转换为24小时格式
问题描述:
从服务器获取的时间就像 2011年7月27日8:35:29 AM
。
The time I get from the server is like Jul 27, 2011 8:35:29 AM
.
我想将其转换为 yyyy-MM-dd HH:mm:ss
。
我也希望转换时间为24小时格式。任何人都可以解决这个问题。我想得到的输出就像 2011-07-27 08:35:29
I also want the converted time to be in 24 hour format. Can any one give solution to this problem. The output I want to get is like 2011-07-27 08:35:29
答
尝试这样:
String dateStr = "Jul 27, 2011 8:35:29 AM";
DateFormat readFormat = new SimpleDateFormat( "MMM dd, yyyy hh:mm:ss aa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = readFormat.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
if (date != null) {
String formattedDate = writeFormat.format(date);
}