如何解决如何使用包含日期和时间的字符串设置DateField值?
| 我有以下字符串\“ 29/05/2012 12:11 PM \” 我想将此值添加到DateField 我这样做是这样的:String dateTime = \"29/05/2012 12:11 PM\";
long initialDateTime = System.currentTimeMillis();
SimpleDateFormat SDF_DateTime = new SimpleDateFormat(\"dd/MM/yyyy hh:mm aa\");
DateField DF_DateTime = new DateField(\"Time: \",initialDateTime,SDF_DateTime,Field.FIELD_LEFT | DrawStyle.LEFT | Field.FOCUSABLE);
DF_DateTime.setTimeZone(TimeZone.getDefault());
DF_DateTime.setDate(stringToDate(dateTime));
add(DF_DateTime);
哪里
public Date stringToDate(String s)
{
Calendar c = Calendar.getInstance(TimeZone.getDefault());
c.set(Calendar.DAY_OF_MONTH,Integer.parseInt(s.substring(0,2)));
c.set(Calendar.MONTH,Integer.parseInt(s.substring(3,5)) - 1);
c.set(Calendar.YEAR,Integer.parseInt(s.substring(6,10)));
c.set(Calendar.HOUR,Integer.parseInt(s.substring(11,13)));
c.set(Calendar.MINUTE,Integer.parseInt(s.substring(14,16)));
c.set(Calendar.SECOND,0);
c.set(Calendar.AM_PM,s.substring(17,19).toLowerCase().equals(\"am\") ? Calendar.AM : Calendar.PM);
return c.getTime();
}
但是该字段显示:\“ 2012/05/30 12:11 \”
而不是“ 2012年5月29日下午12:11”
如果有必要知道,我的设备会在以下时区中设置选项:\“伦敦都柏林(GMT)\”
对正在发生的事情有逻辑上的解释吗?
解决方法
我已经找到了解决方案,这里是给有兴趣的人的:
c.set(Calendar.HOUR,Integer.parseInt(s.substring(11,13)) % 12);
因为在Javadoc中,对于“ 3”,它指定了:
HOUR用于12小时制(0
-11)。中午和午夜用0而不是12表示
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。