带有"Z"的java格式时间戳而不是"+" [英] java format timestamps with "Z" instead of "+"

查看:184
本文介绍了带有"Z"的java格式时间戳而不是"+"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java,我尝试使用SimpleDateFormat使用时区格式化当前日期

Using java I try to format the current date with the timezone using SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSSZ");
sdf.format(new Date());

这给了我结果:

   2021-04-28T13:45:52:308+0300

我想获取带有"Z"的时区格式而不是"+"

I want to get the timezone format with the "Z" instead of "+"

想要的结果:" 2021-04-28T13:45:52:308Z03:00"

我将日期输出写到了文件日志中,该文件日志将由用Go语言编写的telegraf插件解析,该插件期望带有时区的日期具有以下格式: json_time_format ="2006-01-02T15:04:05Z07:00"

I writed the date output in a file log that will be parsed by telegraf plugin writed in Go language that expect date with time zone with the following format : json_time_format = "2006-01-02T15:04:05Z07:00"

有没有一种模式可以允许?

Is there a pattern allows that ?

推荐答案

您误会了. 2006-01-02T15:04:05Z07:00 并不意味着您应该使用 Z 而不是加号(那么您会放什么而不是减号?)这种指定日期和时间格式的方式近似于固定的示例日期和时间(周一1月2 15:04:05 MST 2006)将如何格式化,但这只是一个近似值.具体来说,与UTC的偏移量有关,当偏移量为零且 + hh:mm -hh:mm 时,格式需要 Z 当它不为零时.符合ISO 8601和RFC-3339.您会立即看到,仅给出示例日期和时间的正确格式 2006-01-02T15:04:05-07:00 ,并不会告诉读者偏移量0应指定为 Z .因此,此特定要求以格式指定为 Z07:00 .根据格式化时间或日期[完整指南] (底部的链接),您的特定格式 2006-01-02T15:04:05-0700 表示ISO8601或RFC-3339.

You misunderstood. 2006-01-02T15:04:05Z07:00 does not mean that you should have a Z instead of a plus (what would you put instead of a minus, then?) This way of specifying a date and time format approximates how the fixed example date and time of Mon Jan 2 15:04:05 MST 2006 would be formatted, but it’s only an approximation. Specifically when it comes to the offset from UTC, the format requires Z when the offset is zero and +hh:mm or -hh:mm when it is non-zero. In accordance with ISO 8601 and RFC-3339. You see immediately that just giving the correct formatting of the example date and time, 2006-01-02T15:04:05-07:00, would not tell the reader that offset 0 should be given as Z. Therefore this particular requirement is specified as Z07:00 in the format. According to Format a time or date [complete guide] (link at the bottom), your particular format, 2006-01-02T15:04:05-0700, denotes ISO 8601 or RFC-3339.

因此,您所需要做的就是使用 DateTimeFormat.ISO_OFFSET_DATE_TIME OffsetDateTime.toString().

So all you need to do is use DateTimeFormat.ISO_OFFSET_DATE_TIME or OffsetDateTime.toString().

以下是几个例子.

    String result = OffsetDateTime.now().toString();
    System.out.println(result);

在我所在时区的Java 8上运行时的输出:

Output when running on Java 8 in my time zone just now:

2021-04-29T17:00:55.716 + 02:00

2021-04-29T17:00:55.716+02:00

如果不允许使用小数秒-很好,根据ISO 8601,它是可选的,所以应该是可选的,但如果不是这样的话:

If the fraction of second is not allowed — well, according to ISO 8601 it is optional, so it should be, but if not:

    String result = OffsetDateTime.now().truncatedTo(ChronoUnit.SECONDS)
            .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);

2021-04-29T17:00:55 + 02:00

2021-04-29T17:00:55+02:00

如果您从旧版代码中获得了一个老式的 Date 对象,请在格式化之前对其进行转换:

If you have got an old-fashioned Date object from legacy code, convert it before formatting:

    Date oldfashionedDate = new Date();
    String result = oldfashionedDate.toInstant()
            .atZone(ZoneId.systemDefault())
            .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);

2021-04-29T17:00:55.739 + 02:00

2021-04-29T17:00:55.739+02:00

链接

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆