[英]SimpleDateFormat parsing date differently on some android devices
I am parsing the date as: 我将日期解析为:
public class Consts{
public static final SimpleDateFormat DATE_FORMATTER_WITHOUT_TIME_ZONE = new SimpleDateFormat(
"yyyy-MM-dd HH:mm");
public static final SimpleDateFormat DATE_FORMATTER_2 = new SimpleDateFormat(
"MM-dd-yyyy HH:mm:ss ZZZZ");
}
cDate = new GregorianCalendar();
sDate = new GregorianCalendar();
eDate = new GregorianCalendar();
if (mStartTimeTV.getText().toString().equals("Now")) {
sDate.setTime(cDate.getTime());
} else {
sDate.setTime(Consts.DATE_FORMATTER_WITHOUT_TIME_ZONE
.parse(mStartTimeTV.getText().toString()));
}
if (!mEndTimeTV.getText().toString().equals("")) {
eDate.setTime(Consts.DATE_FORMATTER_WITHOUT_TIME_ZONE
.parse(mEndTimeTV.getText().toString()));
} else {
eDate.setTime(sDate.getTime());
// eDate = sDate;
}
And then i format the date as below before sending it to the server.: 然后在将日期发送到服务器之前,我将日期格式化如下:
request.addProperty("StartTime",
Consts.DATE_FORMATTER_2.format(sDate.getTime()));
request.addProperty("EndTime",
Consts.DATE_FORMATTER_2.format(eDate.getTime()));
But the thing is that on devices running 4.1.2 it is sending the date as: 但事实是,在运行4.1.2的设备上,它发送的日期为:
Start Date: 03-23-2015 21:17:20 +0500 End Date : 10-23-2015 21:15:00 +0500
which throws exception on the server side. 在服务器端抛出异常。
But on the other devices it is sending dates as: 但是在其他设备上,它发送的日期为:
Start Date: 03-23-2015 21:12:13 GMT+05:00 End Date : 03-23-2015 21:16:00 GMT+05:00
which is required. 这是必需的。
Am i doing something wrong? 难道我做错了什么? How can i prevent this problem so that all devices sends the same dates.
我如何预防此问题,以便所有设备发送相同的日期。 (for example
03-23-2015 21:16:00 GMT+05:00
) (例如
03-23-2015 21:16:00 GMT+05:00
)
The difference is caused by different locales set up on devices. 差异是由设备上设置的不同语言环境引起的。
To mitigate the locale differences, you should use particular locale while formatting the date string. 为了减轻语言环境的差异,在格式化日期字符串时应使用特定的语言环境。 Here is a sample from my code, solving the same issue:
这是我的代码中的一个示例,解决了相同的问题:
new SimpleDateFormat(C.FORMAT_DATETIMEZ, Locale.US).format(new Date(time))
You can extend SimpleDateFormat and override its format(...) function as following: 您可以扩展SimpleDateFormat并重写其format(...)函数,如下所示:
public class ExSimpleDateFormat extends SimpleDateFormat{
private static final long serialVersionUID = -8275126788734707527L;
public ExSimpleDateFormat() {
super();
}
public ExSimpleDateFormat(String string, Locale us) {
super(string, us);
}
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, java.text.FieldPosition pos)
{
final StringBuffer buf = super.format(date, toAppendTo, pos);
buf.insert(buf.length() - 2, ':');
return buf;
};
}
And execute following code: 并执行以下代码:
Calendar ca = Calendar.getInstance();
ca.setTimeInMillis(System.currentTimeMillis());
ExSimpleDateFormat exSimpleDateFormat = new ExSimpleDateFormat("dd-MM-yyyy HH:mm:ss 'GMT'Z", Locale.US);
exSimpleDateFormat.setTimeZone(ca.getTimeZone()); //your device timezone which can be GMT+xx:yy or GMT-xx:yy
String desiredTime = exSimpleDateFormat.format(ca.getTime());
Output would be like: 23-03-2015 23:12:52 GMT+05:00 输出将是:23-03-2015 23:12:52 GMT + 05:00
Let me know if it helps. 让我知道是否有帮助。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.