Java SimpleDateFormat.format() 在使用毫秒时返回不正确的日期

标签 java android date datetime

我想将日期字符串从 "2016-09-23T09:14:52.555000000" 格式解析为 "23-SEP-2016" 格式。

这是我的代码:

public class Tester {

    public static void main(String[] args) {
        System.out.println(displayDate("2016-09-23T09:14:52.555000000"));
        System.out.println(displayDate("2016-09-28T11:56:24.552000000"));
        System.out.println(displayDate("2016-09-23T09:29:12.507000000"));
        System.out.println(displayDate("2016-09-26T14:55:02.702000000"));
        System.out.println(displayDate("2016-09-26T09:50:24.880000000"));
        System.out.println(displayDate("2016-09-26T15:20:49.397000000"));
        System.out.println(displayDate("2016-09-26T15:21:21.559000000"));
    }

    public static String displayDate(String dateString) {
        String formattedDateString = "NA";

        if(dateString == null) {
            return formattedDateString;
        }

        SimpleDateFormat oracleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.S");
        SimpleDateFormat dateFormatToDisplay = new SimpleDateFormat("dd-MMM-yyyy");

        try {
            Date date = oracleDateFormat.parse(dateString);
            formattedDateString = dateFormatToDisplay.format(date).toUpperCase();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return formattedDateString;
    }

}

问题是如果我使用这条线

SimpleDateFormat oracleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.S");

输出是(日期值不正确):

29-SEP-2016
04-OCT-2016
29-SEP-2016
04-OCT-2016
06-OCT-2016
01-OCT-2016
03-OCT-2016

而如果使用这一行

SimpleDateFormat oracleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss");

输出是(正确的日期值)

23-SEP-2016
28-SEP-2016
23-SEP-2016
26-SEP-2016
26-SEP-2016
26-SEP-2016
26-SEP-2016

我想知道为什么在格式字符串中添加 "S (Millisecond)" 会导致值不正确。

编辑1:

即使是 "yyyy-MM-dd'T'kk:mm:ss.SSS" 也会返回不正确的值。

编辑2:

我在 Android 应用程序中使用此代码。它在模拟器(API 23)上完美运行。对于某些设备,它显示的日期不正确。可以和Java版本有关吗?

最佳答案

TL;DR:

在 Java 中,它不是几分之一秒,而是几毫秒。不要让它大于 999。

在 Oracle 中更改您的日期格式,以包含 FF3 以生成毫秒的小数部分。

说明

在 Java 中,日期格式中的 S 或者更确切地说 SSS 代表毫秒,即千分之一秒。一秒只有一千毫秒。

Oracle ,日期格式不指定毫秒,而是几分之一秒。

FF [1..9]
Fractional seconds; no radix character is printed. Use the X format element to add the radix character. Use the numbers 1 to 9 after FF to specify the number of digits in the fractional second portion of the datetime value returned. If you do not specify a digit, then Oracle Database uses the precision specified for the datetime datatype or the datatype's default precision. Valid in timestamp and interval formats, but not in DATE formats.

在 Java 中,如果您需要 1/3 秒,那么您无法得到比 333 毫秒更精确的时间。然而,在 Oracle 中,您可以将其表示为 333333 微秒,甚至可能是 333333333 纳秒。

Oracle 允许您指定所需的小数位数,但如果您不这样做,您将获得类型所允许的精度。在你的情况下,这似乎是 9。
然后您在 Java 中的日期格式将其解释为毫秒数。数以百万计的他们。这些是添加到你约会的其余部分。由于一天只有 86,400,000 毫秒,因此任何超过此时间的时间都会为您的日期添加另一天。

示例

让我们看一下您的第一个测试用例,2016-09-23T09:14:52.555000000
555000000 毫秒 = 555000 秒 ≈ 154 小时 ≈ 6 天 10 小时。
在剩下的日期(即 2016 年 9 月 23 日 09:14:52)上加上 6 天 10 小时,应该会让你到 2016 年 9 月 29 日 19:00 左右。更改您的输出格式 (dateFormatToDisplay) 以包含小时数,您会看到发生了什么。

修复

您的 Java 日期格式要求毫秒数不超过 3 位。在 Oracle 中指定小数位数。 FF 使用该类型可用的最大精度,FF3 仅输出 3 个小数位 - 毫秒。
如果您无法更改 Oracle 中使用的日期格式,请在 Java 中将其缩减为三位十进制数字。请注意,任何小于的数字都应该用零填充到三位数的长度; 34.12 被解释为 34 秒和 12 毫秒,而您可能正在寻找 120 毫秒。

关于Java SimpleDateFormat.format() 在使用毫秒时返回不正确的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39764125/

相关文章:

Excel、VBA 和日期

java - JAXB 无法处理包含空格的 XML 元素到 java.net.URI 的映射

java - 在 Java 中读取 3GB 的非常大的 csv 文件的内存有效方法是什么?

Android:MySQL数据库SQL语句

android - 使用 setDefaultUncaughtExceptionHandler (reportDropResult) 捕获未处理的异常时线程卡住

android - 为什么 `ACTION_GATT_DISCONNECTED` 确实需要这么多时间来更新状态?

java - 日期差异包括时区偏移,怎么了?

java - 无法以编程方式连接到蓝牙设备

java - Spring - 请求上下文或安全上下文仅存储访问 token ?

mysql - Swift 3 解析日期字符串