Java - 将 .NET 日期转换为 LocalDateTime

Java - Convert .NET date to LocalDateTime

我正在使用第三方服务,returns 我的日期格式如下:

"EndDate":"\/Date(1487615921387-0300)\/","StartDate":"\/Date(1487608721387-0300)\/"

我的问题是将此日期转换为 LocalDateLocalDateTime。我在这里找到了一些答案,但他们使用的是 joda 时间,所以没有帮助。

好的,首先你应该从你的字符串中提取你的日期我使用一个模式这个想法很简单

public static void main(String[] args) {

    String str = "\"EndDate\":\"\/Date(1487615921387-0300)\/\",\"StartDate\":\"\/Date(1487608721387-0300)\/\"";
    //Get Long from your String between Date( and )
    String start = "Date(", end = ")";
    String regexString = Pattern.quote(start) + "(.*?)" + Pattern.quote(end);
    Pattern pattern = Pattern.compile(regexString);
    Matcher matcher = pattern.matcher(str);
    List<String> res = new ArrayList<>();
    while (matcher.find()) {
        //now we get results like this 1487608721387-0300
        res.add(matcher.group(1));
    }

    //You can change the format like you want
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Date date;
    String[] split;
    for (String s : res) {
        split = s.split("-");
        ///we should to split the String to get the first part 1487608721387
        //then we get Date from this String
        date = new Date(new Long(split[0]));
        //Set time zone to your format i'm not sure if it is correct you can avoid it
        //format.setTimeZone(TimeZone.getTimeZone(split[1]));
        //Show your date
        System.out.println(format.format(date));
    }
}

您需要了解输入数据的含义。

最后一部分 -0300 可能是 offset-from-UTC, a number of hours ahead of or behind UTC。我建议使用带冒号 (-03:00) 的格式,但不带也是可以接受的。您需要知道 plus/minus 符号是指早于还是晚于 UTC。现代协议倾向于在 UTC 之前使用 plus,在 UTC 之后使用 minus,但也有相反的协议。

知道偏移量不是时区。一个time zone is a history of offsets for a particular region with rules for anomalies such as Daylight Saving Time (DST)

第一部分可能是自 epoch reference date. We can guess that your epoch is the commonly used first moment of 1970 in UTC (1970-01-01T00:00:00). But there are at least a couple dozen epochs 以来各种已知软件系统使用的毫秒数。同样,您必须查阅数据来源。

这个 count-from-epoch 与我以前见过的偏移量的特殊组合。这让我感到困惑,因为在没有偏移量的情况下简单地在 UTC 中使用 count-from-epoch 更有意义。如果要显示调整到时区的 date-time,请使用标准 ISO 8601 字符串格式。

我猜你输入的数字是从纪元开始的计数,单位为 UTC 毫秒数。所以我们将它解析为一个 Instant 对象。 Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds(最多九 (9) 位小数)。

String input = "1487615921387-0300";

String inputCount = input.substring ( 0 , 13 ); // zero-based index counting.
long count = Long.parseLong ( inputCount );
Instant instant = Instant.ofEpochMilli ( count );

我们可以将偏移量解析为 ZoneOffset 对象。

String inputOffset = input.substring ( 13 );
ZoneOffset offset = ZoneOffset.of ( inputOffset );

应用 ZoneId to see the same moment as a wall-clock time in another offset as an OffsetDateTime.

OffsetDateTime odt = instant.atOffset ( offset );

看到这个code run live at IdeOne.com

input: 1487615921387-0300

inputMillis: 1487615921387

inputOffset: -0300

count: 1487615921387

instant.toString(): 2017-02-20T18:38:41.387Z

odt.toString(): 2017-02-20T15:38:41.387-03:00

请注意 instantodt 之间的三个小时差异,小时 1815 之间的差异,偏移的影响。仍然是同一时刻,时间轴上的同一点,但在不同的 wall-clock 时间看到。


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time 类.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

在哪里获取java.time类?

  • Java SE 8 and SE 9 及更高版本
    • Built-in。
    • 标准 Java API 的一部分,带有捆绑实施。
    • Java 9 添加了一些小功能和修复。
  • Java SE 6 and SE 7
    • java.time 的大部分功能是 back-ported 到 Java ThreeTen-Backport 中的 6 和 7。
  • Android
    • ThreeTenABP 项目专门针对 Android 改编 ThreeTen-Backport(如上所述)。
    • 参见

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.