当时间区包括SimpleDateFormat的时间太长 [英] SimpleDateFormat takes too long when the time zone is included

查看:189
本文介绍了当时间区包括SimpleDateFormat的时间太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这简单的日期格式

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z");

问题是,当我用这个时间太长的时间转换,在logcat中我看到这样的事情

the problem is when I use this it takes too long to convert the time, in logcat I see something like this

I/Resources( 4284): Loaded time zone names for en in 272ms.
I/Resources( 4284): Loaded time zone names for en in 194ms.
I/Resources( 4284): Loaded time zone names for en in 112ms.
I/Resources( 4284): Loaded time zone names for en in 111ms.
I/Resources( 4284): Loaded time zone names for en in 113ms.
I/Resources( 4284): Loaded time zone names for en in 127ms.
I/Resources( 4284): Loaded time zone names for en in 253ms.
I/Resources( 4284): Loaded time zone names for en in 110ms.
I/Resources( 4284): Loaded time zone names for en in 154ms.
I/Resources( 4284): Loaded time zone names for en in 112ms.

我如何使用简单的数据格式化器,但要加快东西,我不想拿〜150毫秒为每次转换...

How can I use simple date formater but to speed the things up, I do not want to take ~150ms for every conversion...

是否有人之前有这个问题?

Does anybody had this problem before ?

推荐答案

这是由于时区区字符串的延迟初始化。只有第 电话会议将借此长。如果SimpleDateFormat的再次随后使用它的 从缓存中加载,并且不应该花这么长时间了。

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.

在这被改变了它做类加载时并由此开始 活动采取了那些2-3秒钟。这对用户带来更糟糕的影响 比,如果它需要那些秒时,它的实际使用的第一次经历。该 问题是,有没有办法,现在来规避这个问题由于设计 的SimpleDateFormat的API。只有更快的手机可能仅通过少取解决这个问题 时间收集这些字符串。

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.

的缓存发生在该SimpleDateFormat的是使用的DateFormatSymbols。通过 重用例如它可能只需要一次加载蜇伤(为 同样loale)。你也可以创建一个线程,在实例的启动 活性,因此它已经被缓存一旦它的使用。要初始化字符串只需要调用 .hash code(),它并不力初始化缓存。有点快,但不能简单将 是序列化的实例。这也迫使缓存被初始化

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你的进程之前,你需要它。只是分析了AsyncTask的doInBackground一些日期()得到它加载时区的某个时候,它不会影响用户这么多。一旦你的进程初始化,SimpleDateFormat的运行迅速,直到你的进程被终止。

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.

这篇关于当时间区包括SimpleDateFormat的时间太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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