SimpleDateFormat的一些Android设备不同分析日期 [英] SimpleDateFormat parsing date differently on some android devices

查看:188
本文介绍了SimpleDateFormat的一些Android设备不同分析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析日期为:

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()));

但问题是,在设备上运行4.1.2它发送日期:

But the thing is that on devices running 4.1.2 it is sending the date as:

开始日期:2015年3月23日21点17分二十秒0500 截止日期:2015年10月23日21点十五分00秒0500

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:

开始日期:2015年3月23日21:12:13 GMT + 05:00 截止日期:2015年3月23日21时16分零零秒GMT + 05:00

Start Date: 03-23-2015 21:12:13 GMT+05:00 End Date : 03-23-2015 21:16:00 GMT+05:00

其中是必需的。

我是做错了什么?我怎样才能prevent这个问题,使所有设备发送相同的日期。 (例如: 2015年3月23日21时十六分00秒GMT + 05:00

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)

推荐答案

您可以扩展的SimpleDateFormat并覆盖其格式(...)函数如下:

You can extend SimpleDateFormat and override its format(...) function as following:

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;
    };
}

和执行以下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());

输出会是这样:23-03-2015 23时十二分52秒GMT + 05:00

让我知道,如果它帮助。 谢谢

Let me know if it helps. Thanks

这篇关于SimpleDateFormat的一些Android设备不同分析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆