如何检查Calendar实例最初是否是错误的日期
我有一个Calendar实例,通过 javax.xml.bind.DatatypeConverter.parseDateTime
方法。
I have a Calendar instance, parsed from XSD datetime via the javax.xml.bind.DatatypeConverter.parseDateTime
method by JAXB.
在运行时,我在网络服务中,我想知道原始xsd日期时间是否有效(月份< 12,日期< 31,30,29,28,根据月份......等等。示例:这是一个有效日期: 2015-07-30T09:32:05.543 + 02:00
,这不是: 2015-07-35T09 :32:05.543 + 02:00
At runtime, i'm in a web service, and i want to know if the original xsd datetime was valid (month < 12, day < 31, 30, 29, 28 according to month ...ect) or not. Example: this is a valid date: 2015-07-30T09:32:05.543+02:00
and this is not: 2015-07-35T09:32:05.543+02:00
我试图使用 setLenient
服务,但是当原始日期错误时似乎没有引发异常。
I tried to use setLenient
on the instance in the web service, but it doesn't seem to raise an exception when the original date was wrong.
有没有办法做到这一点?我的猜测是告诉JAXB在 global.jaxb
文件中以正确的方式执行此操作,这是我现在使用的文件:
Is there any way to do it? My guess is to tell JAXB to do it the right way in the global.jaxb
file, here's the one i use right now:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema"
jaxb:extensionBindingPrefixes="xjc">
<jaxb:globalBindings>
<xjc:simple/>
<xjc:serializable uid="-1"/>
<jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
printMethod="javax.xml.bind.DatatypeConverter.printDateTime"/>
</jaxb:globalBindings>
</jaxb:bindings>
任何帮助将不胜感激。谢谢。
Any help would be appreciated. Thanks.
tl; dr
tl;dr
OffsetDateTime.parse( "2015-07-35T09:32:05.543+02:00" )
… catch ( DateTimeParseException e )
java.time
麻烦的旧日期时间类,例如 java.util.Date
, java.util.Calendar
和 java.text.SimpleDateFormat
现在遗产,取而代之的是 java.time 类Java 9.
java.time
The troublesome old date-time classes such as java.util.Date
, java.util.Calendar
, and java.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 & Java 9.
同样, Joda-时间项目现在处于维护模式,团队建议迁移到 java.time 类。
Likewise, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.
要确定无效的日期时间字符串,请尝试解析并捕获异常。鉴于您的输入具有从UTC而非时区的偏移量,请解析为 OffsetDateTime
个对象。无效输入抛出 DateTimeParseException
。
To determine invalid date-time string, attempt to parse and trap for exception. Given that your inputs have an offset-from-UTC but not a time zone, parse as OffsetDateTime
objects. Invalid inputs throw DateTimeParseException
.
String inputGood = "2015-07-30T09:32:05.543+02:00" ;
String inputBad = "2015-07-35T09:32:05.543+02:00" ;
try{
// Good
OffsetDateTime odtGood = OffsetDateTime.parse( inputGood ) ;
System.out.println( "odtGood.toString(): " + odtGood ) ;
// Bad
OffsetDateTime odtBad = OffsetDateTime.parse( inputBad ) ;
System.out.println( "odtBad.toString(): " + odtBad ) ;
} catch ( DateTimeParseException e ) {
System.out.println( e ) ;
}
odtGood.toString():2015-07 -30T09:32:05.543 + 02:00
odtGood.toString(): 2015-07-30T09:32:05.543+02:00
java.time.format.DateTimeParseException:Text'2015-07-35T09:32:05.543 + 02:00'可能不被解析:DayOfMonth的值无效(有效值1 - 28/31):35
java.time.format.DateTimeParseException: Text '2015-07-35T09:32:05.543+02:00' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 35
关于java.time
java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如 java.util.Date
, 日历
,& SimpleDateFormat
。
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
Joda-Time 项目,现在在维护模式,建议迁移到 java.time 类。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参阅 Oracle教程。并搜索Stack Overflow以获取许多示例和解释。规范是 JSR 310 。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
从哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 , Java SE 9 ,以及之后
- 内置。
- 带有捆绑实现的标准Java API的一部分。
- Java 9添加了一些小功能和修复。
-
Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- 大部分java.time功能都被反向移植到Java 6& 7 ThreeTen-Backport 。
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- 更新版本的Android捆绑实现java.time类。
- 对于早期的Android, ThreeTenABP 项目适应 ThreeTen-Backport (如上所述)。请参阅 如何使用ThreeTenABP ...... 。
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
ThreeTen-Extra 项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在这里找到一些有用的课程,例如
Interval
,YearWeek
,YearQuarter
,更多。