Closed geniusjoe closed 2 years ago
TestDemo:
@Test
public void testDemo() {
final DateParser dp = DateParser.newBuilder().build();
System.out.println(String.format("current timezone = %s", ZoneId.systemDefault()));
System.out.println();
String date = "2022-09-09T09:30:57-00:00";
System.out.println(String.format("parserDate [%s] result [%s]", date, dp.parseDate(date).toString()));
System.out.println(String.format("parseDateTime [%s] result [%s]", date, dp.parseDateTime(date).toString()));
System.out.println(String.format("parseOffsetDateTime [%s] result [%s]", date, dp.parseOffsetDateTime(date).toString()));
System.out.println();
// convert date to unix timestamp format
date = "1662715857";
System.out.println(String.format("parserDate [%s] result [%s]", date, dp.parseDate(date).toString()));
System.out.println(String.format("parseDateTime [%s] result [%s]", date, dp.parseDateTime(date).toString()));
System.out.println(String.format("parseOffsetDateTime [%s] result [%s]", date, dp.parseOffsetDateTime(date).toString()));
}
result before converting to default timezone:
current timezone = Asia/Shanghai
parserDate [2022-09-09T09:30:57-00:00] result [Fri Sep 09 17:30:57 CST 2022]
parseDateTime [2022-09-09T09:30:57-00:00] result [2022-09-09T17:30:57]
parseOffsetDateTime [2022-09-09T09:30:57-00:00] result [2022-09-09T09:30:57Z]
parserDate [1662715857] result [Fri Sep 09 17:30:57 CST 2022]
parseDateTime [1662715857] result [2022-09-09T09:30:57] <-------- notice here
parseOffsetDateTime [1662715857] result [2022-09-09T09:30:57Z]
result after converting to default timezone:
current timezone = Asia/Shanghai
parserDate [2022-09-09T09:30:57-00:00] result [Fri Sep 09 17:30:57 CST 2022]
parseDateTime [2022-09-09T09:30:57-00:00] result [2022-09-09T17:30:57]
parseOffsetDateTime [2022-09-09T09:30:57-00:00] result [2022-09-09T09:30:57Z]
parserDate [1662715857] result [Fri Sep 09 17:30:57 CST 2022]
parseDateTime [1662715857] result [2022-09-09T17:30:57] <-------- notice here
parseOffsetDateTime [1662715857] result [2022-09-09T09:30:57Z]
@sisyphsu Hello, would you mind reviewing this pr ? 😁
Thanks for your PR, using default timezone is more reasonable.
This PR has been merged into new version 1.0.11
~
Do UNIX timestamps change across time zones?
As mentioned above, the unix timestamp does not have timeZone concept. So we need to set a demand timeZone when converting from unix timestamp to java
localDateTime
type. Example below:When it comes to
dateparser
, we do not need to set timezone when using basic usage mode. And a unix timestamp will be converted tolocalDateTime
using UTC+0 timezone currently. Because thelocalDateTime
is designed for storing local time, I think it is better to convert the timestamp tolocalDateTime
with local system timezone.