【发布时间】:2018-07-16 03:03:51
【问题描述】:
需要将时间转换成指定格式,但得到错误的结果。请帮忙
DateFormat readFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss.sss");
DateFormat writeFormat = new SimpleDateFormat( "hh:mm aa"); //Expecting like 05:00
String from = "2018-02-05 17:00:52.503";
String t0 = "2018-02-05 17:00:55.372";
Date date = null; //IST
Date date2=null;
try {
date = readFormat.parse(from);
date2 =readFormat.parse(to);
} catch (ParseException e) {
e.printStackTrace();
}
if (date != null) {
from = writeFormat.format(date); //Expecting a result of 05.00 pm
to = writeFormat.format(date2); //Expecting a result of 05:00 pm
}
-
列表项##需要这样输出##
/*但是得到一个输出为:
from= "05:08 pm" instead of 05:00 pm to = "05:06 pm" instead of 05:00 pm*/
【问题讨论】:
-
试试
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); -
只需阅读您自己的标题即可。 :-) 您需要仔细观察这些格式模式字母的大小写。在应该使用大写字母的地方使用小写字母非常容易,反之亦然。你需要大写的
HH和大写的SSS。 -
顺便考虑一下扔掉长期过时且臭名昭著的麻烦
SimpleDateFormat和朋友,并将ThreeTenABP添加到您的Android项目中,以便使用java.time,现代Java日期和时间API .使用起来感觉好多了。
标签: java android date date-formatting date-parsing