Talend:时区java

问题描述 投票:0回答:2

我正在尝试学习Talend Open Studio。我有一列包含“ 2019-09-17 08:42:09 +0400”之类的字符串,我想用java组件而不是tmap将其转换为日期时间,但要在我的时间中加上“ +0400”。我尝试了很多类似LocalDate的方法,但是没有用。

[如果有人知道该怎么做,我将不胜感激。非常感谢。

java datetime timezone talend
2个回答
1
投票
要以给定格式解析日期,可以使用SimpleDateFormat

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); Date date = format.parse("2019-09-17 08:42:09 +0400"); // from string to date String dateAsString = format.format(date); // from date to string


0
投票
java.time

DateTimeFormatter formatter = new DateTimeFormatterBuilder() .append(DateTimeFormatter.ISO_LOCAL_DATE) .appendLiteral(' ') .append(DateTimeFormatter.ISO_LOCAL_TIME) .appendLiteral(' ') .appendOffset("+HHmm", "+0000") .toFormatter(); String stringFromTalendCol = "2019-09-17 08:42:09 +0400"; OffsetDateTime javaDateTime = OffsetDateTime.parse(stringFromTalendCol, formatter); System.out.println(javaDateTime);
此代码段的输出是:

2019-09-17T08:42:09 + 04:00

如果您精简代码,也可以只在一行中定义格式化程序:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss xx");

我的爱好是重用java.time提供的构建基块,这就是为什么我首先提出更长的选项的原因。在两种情况下结果都是相同的。

LocalDate还不够

您提到您尝试过LocalDateLocalDate是没有时间,没有时区或UTC偏移的日期,因此它不能代表您提出问题的日期和时间,这可能是您尝试失败的原因之一。

您的字符串具有日期,一天中的时间和UTC偏移量。 OffsetDateTime是表示此信息的完全正确的类。

+ 0400的偏移量表示,与UTC相比,已将时间增加了4小时0分钟。因此,您的时间点等于2019-09-17T04:42:09Z,其中Z表示UTC或偏移量为零。

链接

Oracle tutorial: Date Time解释如何使用java.time。

© www.soinside.com 2019 - 2024. All rights reserved.