2

我正在尝试将服务器时间(GMT+09:00)转换为本地时间(客户端的任何区域)。

我试过了java.time。但是,它至少需要 minSdk API26。然后,我寻找替代方法

所以,我使用了 ThreeTenABP库。

我得到了这个例外。

java.lang.ExceptionInInitializerError
        at org.threeten.bp.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:121)
        at org.threeten.bp.ZoneRegion.ofId(ZoneRegion.java:143)
        at org.threeten.bp.ZoneId.of(ZoneId.java:358)

onBindViewHolder()适配器中。

        val item = datas?.get(position)

        AndroidThreeTen.init(context)

        val checkedDate = item?.datetime
        val serverTimeZone = ZoneId.of("Asia/Tokyo")
        val serverFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")
        val clientTimeZone = ZoneId.systemDefault()

        val serverDateTime = LocalDateTime.parse(checkedDate, serverFormatter).atZone(serverTimeZone)
        val clientDateTime = serverDateTime.withZoneSameInstant(clientTimeZone)

另外,它似乎并不提供所有时区。如果我查看 ZoneId,我会在下面看到这些:

    static {
        Map<String, String> base = new HashMap();
        base.put("ACT", "Australia/Darwin");
        base.put("AET", "Australia/Sydney");
        base.put("AGT", "America/Argentina/Buenos_Aires");
        base.put("ART", "Africa/Cairo");
        base.put("AST", "America/Anchorage");
        base.put("BET", "America/Sao_Paulo");
        base.put("BST", "Asia/Dhaka");
        base.put("CAT", "Africa/Harare");
        base.put("CNT", "America/St_Johns");
        base.put("CST", "America/Chicago");
        base.put("CTT", "Asia/Shanghai");
        base.put("EAT", "Africa/Addis_Ababa");
        base.put("ECT", "Europe/Paris");
        base.put("IET", "America/Indiana/Indianapolis");
        base.put("IST", "Asia/Kolkata");
        base.put("JST", "Asia/Tokyo");
        base.put("MIT", "Pacific/Apia");
        base.put("NET", "Asia/Yerevan");
        base.put("NST", "Pacific/Auckland");
        base.put("PLT", "Asia/Karachi");
        base.put("PNT", "America/Phoenix");
        base.put("PRT", "America/Puerto_Rico");
        base.put("PST", "America/Los_Angeles");
        base.put("SST", "Pacific/Guadalcanal");
        base.put("VST", "Asia/Ho_Chi_Minh");
        base.put("EST", "-05:00");
        base.put("MST", "-07:00");
        base.put("HST", "-10:00");
        SHORT_IDS = Collections.unmodifiableMap(base);
    }

所以,当我在韩国首尔(“亚洲/首尔”)或地图不包括的其他地方时base,我不知道它是否正常工作。如果我删除AndroidThreeTen.init(context),我会在下面得到这个:

org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered
        at org.threeten.bp.zone.ZoneRulesProvider.getProvider(ZoneRulesProvider.java:165)
        at org.threeten.bp.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:122)
        at org.threeten.bp.ZoneRegion.ofId(ZoneRegion.java:143)
        at org.threeten.bp.ZoneId.of(ZoneId.java:358)
4

1 回答 1

1

您需要AndroidThreeTen.init(context)在 ApplicationonCreate()方法中调用,而不是在onBindViewHolder()

class MyApp : Application() {

    override fun onCreate() {
        super.onCreate()

        AndroidThreeTen.init(this@App)
    }
}

请记住将您的应用注册到清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.app">

    <application
            android:name=".MyApp"
                <!-- .... -->
            >

    </application>

</manifest>
于 2019-06-25T03:10:04.593 回答