【问题标题】:Getting ParseException: Unparseable date exception in Kotlin获取 ParseException:Kotlin 中不可解析的日期异常
【发布时间】:2021-05-02 09:56:41
【问题描述】:

我正在尝试以以下格式将传入的 ISO 日期(如 (2021-02-15T00:00:00.000Z))显示到 Textview:“dd MM yyyy”。 但是解析日期时出现以下错误。

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
         Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
         Caused by: java.text.ParseException: Unparseable date: "2021-02-15T00:00:00.000Z"
            at java.text.DateFormat.parse(DateFormat.java:362)

这是 Kotlin 代码:

val format = SimpleDateFormat(
    "yyyy-MM-dd'T'HH:mm:ss'Z'",
    Locale.getDefault()
)
val initialConvertedDate = format.parse(customfield.value)

val simpleDateFormat = SimpleDateFormat(
    "dd MM yyyy",
    Locale.getDefault()
)
val finalDate = simpleDateFormat.format(initialConvertedDate)

tv2.text = finalDate

【问题讨论】:

标签: android date kotlin simpledateformat


【解决方案1】:

使用yyyy-MM-dd'T'HH:mm:ss.SSSXXX 模式,其中X 真正代表timezone offset。在您的日期时间字符串中,时区偏移量为 Z,它代表 Zulu 并指定 UTC(时区偏移量为 +00:00 小时)。

你使用的格式有两个问题:

  1. 它使Z 成为字符文字,因此它不代表时区偏移量。
  2. 它没有指定秒的分数,即SSS

演示:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    public static void main(String args[]) throws ParseException {
        String strDateTime = "2021-02-15T00:00:00.000Z";
        SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.getDefault());
        Date date = sdfInput.parse(strDateTime);
        SimpleDateFormat sdfOutput = new SimpleDateFormat("dd MM yyyy");
        sdfOutput.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        String formatted = sdfOutput.format(date);
        System.out.println(formatted);
    }
}

输出:

15 02 2021

请注意,旧的日期时间 API(java.util 日期时间类型及其格式化 API,SimpleDateFormat)已过时且容易出错。建议完全停止使用,改用java.timemodern date-time API*

使用现代日期时间 API 的演示:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String args[]) {
        String strDateTime = "2021-02-15T00:00:00.000Z";
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
        System.out.println(odt);

        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("dd MM yyyy");
        String formatted = dtfOutput.format(odt);
        System.out.println(formatted);
    }
}

输出:

2021-02-15T00:00Z
15 02 2021

请注意,现代日期时间 API 基于 ISO-8601,因此您无需显式使用 DateTimeFormatter 来解析已采用 ISO-8601 格式的日期时间字符串。

Trail: Date Time 了解有关 modern date-time API* 的更多信息。


* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 和 7 . 如果您正在为一个 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-08
    • 2011-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多