带有时区的Java日期验证器 [英] Java Date Validator with Time Zone

查看:158
本文介绍了带有时区的Java日期验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当日期中包含EDT/EST时,我们是否有任何API可以验证日期是否有效.我已经尝试过使用Java Java SimpleDateFormat进行Joda,但是我无法解析日期.

Do we have any API's to validate if a date is valid or not when date is having EDT/EST in it. I have tried with Joda, Java SimpleDateFormat but i am not able to parse the date.

以下是我得到的"2017/09/25 16:18:15.099 -0400 EDT"的示例格式.

Below is the sample format i am getting "2017/09/25 16:18:15.099 -0400 EDT".

感谢您的帮助.

谢谢, sk

推荐答案

您是否尝试过阅读文档?

Did you try reading the documentation?

来自 DateTimeFormatter的文档:

 z     time-zone name   zone-name     Pacific Standard Time; PST
 x     zone-offset      offset-x      +0000; -08; -0830; -08:30; -083015; -08:30:15;

区域名称:这将输出时区ID的显示名称.如果字母的数量为一,二或三,则简称为 输出. …

Zone names: This outputs the display name of the time-zone ID. If the count of letters is one, two or three, then the short name is output. …

偏移X和x :这会根据图案字母的数量来格式化偏移. …两个字母输出小时和分钟,不带 冒号,例如"+0130". …

Offset X and x: This formats the offset based on the number of pattern letters. … Two letters outputs the hour and minute, without a colon, such as '+0130'. …

所以我们尝试一下:

    ZonedDateTime zdt = ZonedDateTime.parse("2017/09/25 16:18:15.099 -0400 EDT",
            DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss.SSS xx zzz", Locale.ENGLISH));

这会产生2017-09-25T16:18:15.099-04:00[America/New_York]ZonedDateTime.

验证

在上面的代码中,ZonedDateTime从字符串中获取时区,并忽略偏移量.为了验证两者是否一致,我们可以将相同的字符串解析为ZonedDateTimeOffsetDateTime并进行比较.后者获取偏移量,并忽略时区缩写,因此可以使用:

In the code above ZonedDateTime picks up the time zone from your string and ignores the offset. To validate that the two agree, we can parse the same string both into a ZonedDateTime and into an OffsetDateTime and compare. The latter picks up the offset and ignores the time zone abbreviation, so this will work:

    String inputDateTimeWithZone = "2017/09/25 16:18:15.099 -0400 EDT";
    DateTimeFormatter formatterWithOffsetAndZone 
            = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss.SSS xx zzz", Locale.ENGLISH);
    ZonedDateTime zdt = ZonedDateTime.parse(inputDateTimeWithZone,
            formatterWithOffsetAndZone);
    OffsetDateTime odt = OffsetDateTime.parse(inputDateTimeWithZone, 
            formatterWithOffsetAndZone);
    if (! zdt.toOffsetDateTime().equals(odt)) {
        System.err.println("Offset does not match: is " + odt.getOffset()
                + ", but time zone " + zdt.getZone() + " implies offset " + zdt.getOffset());
    }

请避免使用三个字母的时区缩写

EST和EDT也可以分别用于澳大利亚东部标准时间和夏令时.三个字母的时区缩写不统一,通常不明确.如果您可以获取带有 region/city 格式的时区ID的字符串(例如America/New_York),或者只是删除缩写并仅依靠偏移量,则将更加安全,因为这是明确的.

EST and EDT may also be used about Australian Eastern Standard and Daylight Time, respectively. Three letter time zone abbreviations are not standardized and are often ambiguous. It would be safer if you either could get a string with a time zone ID in the region/city format (like America/New_York) or you simply stripped off the abbreviation and relied on the offset alone, since this is unambiguous.

现代的Java日期和时间API

今天,我建议在Joda-Time上使用java.time,即AKA JSR-310,当然也应该在已经过时的类SimpleDateFormatDate上.对我来说,使用它非常好,当然也不会有旧课程带来的惊喜.它内置于Java 8及更高版本中.如果使用Java 6或7,一开始可能会考虑使用Joda-Time,但是如果您准备接受外部依赖,为什么不考虑使用

Today I recommend java.time, AKA JSR-310, over Joda-Time and certainly over the long outdated classes SimpleDateFormat and Date. To me it’s proven very nice to work with, and it certainly doesn’t come with the surprises of the old classes. It’s built-in with Java 8 and later. If using Java 6 or 7, you may be tempted to consider Joda-Time at first, but if you’re ready to accept an external dependency, why not take the ThreeTen Backport, the backport of java.time to Java 6 and 7?

这篇关于带有时区的Java日期验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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