Sure, here’s the translation: SimpleDateFormat 用于时区偏移量 +AA:BB 的问题

huangapple go评论77阅读模式
英文:

SimpleDateFormat question for timezone offset +AA:BB

问题

我目前正在使用以下的SimpleDateFormat模式:

String DATE_TIME_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss,SSSXXX";

这个模式运行良好,然而一些树莓派的Java实现无法正确识别它:

timestamp 2020-01-21T09:41:45,434Z

在大多数情况下,这不会成为问题,但对于一些树莓派来说,偏移量存在问题;我不希望出现这种情况。是否有另一种具有相同偏移量格式(+/-HH:mm)的替代模式可行?我尝试过各种模式,但似乎没有产生相同的输出。

我还使用了以下工具来搜索这样的模式:https://javadevtools.com/simpledateformat,但没有结果。

注意:此格式的示例输出是1997-07-16T19:20:30,45+01:00,偏移量中有一个冒号。

英文:

I am currently using the following SimpleDateFormat pattern:
String DATE_TIME_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss,SSSXXX";

This works fine, however some raspberry Pi java implementations don't recognize it properly:

timestamp 2020-01-21T09:41:45,434Z

In most cases, this won't be an issue, however the offset is buggy for some raspberry PIs; I don't want that. Is there an alternative pattern with the same offset format (+/-HH:mm) that could work? I've tried all kinds of patterns, but none seem to produce the same output.

I also used the following tool to search for such a pattern: https://javadevtools.com/simpledateformat , though it was fruitless.

NOTE: An example output of this format is 1997-07-16T19:20:30,45+01:00 , with a colon in the offset.

答案1

得分: 2

以下是您要翻译的内容:

如果您正在使用 java.time,尤其是两个类 java.time.OffsetDateTime(模式符号在此JavaDoc中有解释)和 java.time.format.DateTimeFormatter,您或者您的树莓派将能够正确解析该时间戳(其格式很奇怪,使用逗号将秒的小数部分与秒数分开)。

以下示例解析了您的 timestamp 并输出默认格式:

public static void main(String[] args) {
    String timestamp = "1997-07-16T19:20:30,45+01:00";
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss[,SSS]xxx");
    OffsetDateTime odt = OffsetDateTime.parse(timestamp, dtf);
    System.out.println(odt);
}

输出:

1997-07-16T19:20:30.450+01:00
英文:

If you were using java.time, especially the two classes java.time.OffsetDateTime (pattern symbols are explained in this JavaDoc) and java.time.format.DateTimeFormatter, you or your Raspberry Pi would be able to correctly parse the timestamp (which has a strange format using a comma to separate fractions of second from the seconds).

The following example parses your timestamp and outputs the default format:

public static void main(String[] args) {
	String timestamp = "1997-07-16T19:20:30,45+01:00";
	DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss[,SSS]xxx");
	OffsetDateTime odt = OffsetDateTime.parse(timestamp, dtf);
	System.out.println(odt);
}

Output:

1997-07-16T19:20:30.450+01:00

答案2

得分: 0

我确认这不是一个树莓派问题。我将本地时区切换为UTC,并运行了以下示例:

long current = System.currentTimeMillis(); 
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss,SSSXXX"); 
Date date = new Date(current);
String parsed = format.format(date);
System.out.println(parsed);
2020-08-31T15:05:27,872Z

在 Windows 10 上出现了 Z。我错过了ISO规范的那部分内容。看起来我必须为这种情况找到解决方法 :)。谢谢大家!

英文:

I confirm that this is not a Pi issue. I switched my local time zone to UTC and ran the following example:

long current = System.currentTimeMillis(); 
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss,SSSXXX"); 
Date date = new Date(current);
String parsed = format.format(date);
System.out.println(parsed);
2020-08-31T15:05:27,872Z

And the Z appeared, on Windows 10. I have missed that part of the ISO spec. It seems I have to workaround my tests for this situation :). Thanks everyone!

huangapple
  • 本文由 发表于 2020年8月31日 22:36:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63672881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定