I am trying to convert "yyyy-MM-dd'T'HHmmssZZ" to unix time in my app. This is my code:

public String getCreatedAt() {
    String formattedCreatedAt = twitterCreatedAt.replace(":", "");
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd'T'HHmmssZZ");
    try {
        formattedCreatedAt = Long.toString(dateFormat.parse(formattedCreatedAt).getTime());
        formattedCreatedAt = formattedCreatedAt.substring(0, formattedCreatedAt.length() - 3);
    } catch (ParseException e1) {
        e1.printStackTrace();
        return "0";
    }
    return formattedCreatedAt;

}

My (weird) problem is that when the device is in english all is OK but when I am changing the device language to spanish in galaxy s2 with android 2.3.6 the line of code formattedCreatedAt = Long.toString(dateFormat.parse(formattedCreatedAt).getTime()); is taking a lot more time to process. Why could it be? Thanks!

This is the part that is taking the long time:

dateFormat.parse(formattedCreatedAt);
有帮助吗?

解决方案

According to this site :

SimpleDateFormat, the first time you try parsing (or, presumably, formatting) a date, will load in all the timezone data for your locale. This will take 2-3 seconds. It is hoped that this will be fixed in some future edition of Android.

In the interim, consider using AsyncTask to "warm up" SimpleDateFormat in your process before you need it. Just parse some date in the AsyncTask doInBackground() to get it to load the timezones sometime when it will not impact the user so much. Once initialized in your process, SimpleDateFormat will run quickly until your process is terminated.

There is an open issue and you can find an explanation :

This is due to the lazy initialization of the timezone zone strings. Only the first call will take this long. If the SimpleDateFormat is used again afterwards it's loaded from cache and shouldn't take that long anymore.

Before this was changed it was done when the class was loaded and thus the start of an activity took those 2-3 seconds longer. This had a much worse impact on the user experience than if it takes those seconds when it's actually used the first time. The problem is that there's no way right now to circumvent this issue due to the design of the SimpleDateFormat api. Only faster phones might fix this by just taking less time to collect those strings.

The caching happens in the DateFormatSymbols that SimpleDateFormat is using. By reusing that instance it's possible to only have to load the stings once (for the same loale). You could also create that Instance in a thread at the startup of the activity so that it's already cached once it's used. To init the strings just call .hashCode() which does force initialize the cache. A bit faster but less simple would be to serialize the instance. This also forces the cache to be initialized.

Since this happens only the first time your function is called (due to the initialization of the timezone strings), I would recommend (as they said) to use an AsyncTask to "pre-load" the SimpleDateFormat. I think it will fix your problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top