android - 在西类牙语中,Date.getTime() 需要花费很多时间来处理

标签 android date android-sdk-2.3 gettime

我正在尝试在我的应用程序中将 "yyyy-MM-dd'T'HHmmssZZ" 转换为 unix 时间。 这是我的代码:

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;

}

我的(奇怪的)问题是,当设备为英语时,一切正常,但是当我在带有 android 2.3.6 的 Galaxy s2 中将设备语言更改为西类牙语时,代码行 formattedCreatedAt = Long.toString( dateFormat.parse(formattedCreatedAt).getTime()); 需要花费更多时间来处理。为什么会这样?谢谢!

这是花费很长时间的部分:

dateFormat.parse(formattedCreatedAt);

最佳答案

根据这个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.

有一个 open issue你可以找到解释:

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.

由于这种情况仅在第一次调用函数时发生(由于时区字符串的初始化),我建议(正如他们所说)使用 AsyncTask “预加载”SimpleDateFormat。我想这会解决你的问题。

关于android - 在西类牙语中,Date.getTime() 需要花费很多时间来处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17019471/

相关文章:

android - Gradle 4.1 在发布构建完成之前进行 crashlytics 上传

java - 优先队列中的删除

android - 在 MVP 模式中,了解 Activity/上下文的演示者是不是一个坏主意?

java - 导入的 Android native 模块始终为 'undefined'

Javascript 函数检查 JSON 对象上是否缺少日期

javascript - 如何将 Unix 时间戳转换为 Jalali/Shamsi/Persian 格式?

android - StrictMode 的 DEVELOPER_MODE 定义

java - 在 ArrayList 中按日期对 ArrayList 列表进行排序

android - Flutter Android SDK 版本 28 错误,但我使用的是 30