在java中我需要以这种格式定义日期1999-05-31T13:20:00-05:00 [英] in java I need define date in this format 1999-05-31T13:20:00-05:00

查看:237
本文介绍了在java中我需要以这种格式定义日期1999-05-31T13:20:00-05:00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以这种格式定义日期 1999-05-31T13:20:00-05:00

I need to define date in this format 1999-05-31T13:20:00-05:00

我使用下面的代码

SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ssZ");
sdf.setTimeZone(TimeZone.getTimeZone("EST"));

String date = sdf.format(new Date());
System.out.println(date);

但是生成日期的格式为 2017-04-117T08:28: 46-0500 (在时区中缺少分号)

but it is generating date in format 2017-04-117T08:28:46-0500 (missing semicolon in timezone)

定义日期后,我必须创建一个具有相同日期的XMLGregorianCalendar实例。

After defining the date I have to create an instance of XMLGregorianCalendar with same date.

推荐答案

我很惊讶你在搜索时没找到答案。无论如何,这很容易。但首先要注意两个字:

I was surprised you didn’t find the answer when searching for it. Anyway, it’s easy. Two words of caution first, though:


  • 跳过三个字母的时区缩写。许多人都很暧昧。虽然我认为EST只意味着东部标准时间,但这不是一个完整的时区,因为(大部分?)该区域现在在EDT上,这并不能很清楚你想要什么结果。

  • 如果你有任何办法,可以跳过老式的课程 SimpleDateFormat TimeZone 日期 java.time 中的新类通常更好用。

  • Skip the three-letter time zone abbreviations. Many are ambiguous. While I think EST only means Eastern Standard Time, this isn’t a full time zone, since (most of?) that zone is on EDT now, which does not make it very clear what result you want.
  • If there’s any way you can, skip the old-fashioned classes SimpleDateFormat, TimeZone and Date. The new classes in java.time are generally much nicer to work with.

您要求的格式正是 DateTimeFormatter.ISO_OFFSET_DATE_TIME 正在给你。如果您打算为东部时区的用户设置适当的格式,请使用:

The format you are asking for is exactly what DateTimeFormatter.ISO_OFFSET_DATE_TIME is giving you. If you meant to have the time formatted suitably for a user in the Eastern time zone, use:

    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("America/New_York"))
            .truncatedTo(ChronoUnit.SECONDS);
    System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

请注意使用明确的时区ID和上述格式化程序。格式将打印毫秒(甚至更小),如果有的话,你没有要求。所以我有点作弊:我把时间缩短到整秒。现在这打印如下:

Note the use of the unambiguous time zone ID and of the mentioned formatter. The format will print milliseconds (and even smaller) if there are any, which you didn’t ask for. So I have cheated a bit: I am truncating the time to whole seconds. Now this prints something like:

2017-04-27T10:11:33-04:00

如果你想要偏移 -05:00 ,那就更容易了:

If instead you wanted the offset -05:00, that’s even easier:

    OffsetDateTime now = OffsetDateTime.now(ZoneOffset.ofHours(-5))
            .truncatedTo(ChronoUnit.SECONDS);
    System.out.println(now);

这会以相同的格式打印某些内容,但具有所需的偏移量:

This prints something in the same format, but with the desired offset:

2017-04-27T09:11:33-05:00

toString 方法 OffsetDateTime 为您提供所需的格式。当然,如果您愿意,可以使用与以前相同的格式化程序。

The toString method of OffsetDateTime gives you the format you want. Of course, if you prefer, you can use the same formatter as before.

如果根据您的喜好截断到秒有点太多,那么您当然可以使用没有毫秒的格式化程序:

If truncating to seconds is a bit too much cheating for your taste, you may of course use a formatter without milliseconds:

    DateTimeFormatter isoNoSeconds = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXXX");
    System.out.println(now.format(isoNoSeconds));

这适用于 ZonedDateTime OffsetDateTime ,即上述两个片段。

This will work with both a ZonedDateTime and an OffsetDateTime, that is, with both of the above snippets.

这篇关于在java中我需要以这种格式定义日期1999-05-31T13:20:00-05:00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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