检查xsd datetime在转换为Java对象之前是否有定义的时区 [英] Check if xsd datetime had a defined timezone before conversion to Java object

查看:786
本文介绍了检查xsd datetime在转换为Java对象之前是否有定义的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是topo,我有从XSD文件生成的Java类的JAXB。对于 xsd:datetime 类型,转换通过 public static Calendar javax.xml.bind.DatatypeConverter.parseDateTime(String lexicalXSDDateTime) 方法。

Here's the topo, i have JAXB generated java classes from XSD files. For xsd:datetime type, the transformation goes through the public static Calendar javax.xml.bind.DatatypeConverter.parseDateTime(String lexicalXSDDateTime) method.

我无法访问日期的字符串值(xml文件中的一个),我所有的权限都是生成的日历对象,我想知道的是,是否在日期的原始字符串值中,是否定义了时区。

I do not have access to the string value of the date (the one in the xml file), all i have access to is the generated Calendar object, and what i want to know, is whether in the original string value of the date, a timezone was defined or not.

例如,这个字符串 2015-07-29T11:17:02.428 + 02:00 有一个定义的时区,其中这个 2015-07-29T11:17:02.428 有一个未定义的,我想知道的是哪个生成 Calendar 一个定义的时区。

For example, this string 2015-07-29T11:17:02.428+02:00 has a defined timezone, where this one 2015-07-29T11:17:02.428 have an undefined one, and all i want to know is which generated Calendar object had originally a defined timezone.

我尝试了 Calendar.getTimeZone()方法,但似乎xsd到java转换不会使时区无效。

I tried the Calendar.getTimeZone() method, but it seems that the xsd to java transformation doesn't nullify the timezone.

例如,以下代码

System.out.println(DatatypeConverter.parseDateTime("2015-07-29T11:17:02.428+02:00").getTimeZone());
System.out.println(DatatypeConverter.parseDateTime("2015-07-29T11:17:02.428").getTimeZone());

输出此


sun.util.calendar.ZoneInfo [id =GMT + 02:00,offset = 7200000,dstSavings = 0,useDaylight = false,transitions = 0,lastRule = null]

sun.util.calendar.ZoneInfo[id="GMT+02:00",offset=7200000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

sun.util.calendar.ZoneInfo [id =Europe / Paris,offset = 3600000,dstSavings = 3600000,useDaylight = true,transitions = 184,lastRule = java.util.SimpleTimeZone [id = Europe /巴黎,offset = 3600000,dstSavings = 3600000,useDaylight = true,startYear = 0,startMode = 2,startMonth = 2,startDay = -1,startDayOfWeek = 1,startTime = 3600000,startTimeMode = 2,endMode = 2,endMonth = 9 ,endDay = -1,endDayOfWeek = 1,endTime = 3600000,endTimeMode = 2]]

sun.util.calendar.ZoneInfo[id="Europe/Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]


推荐答案

p>我在另一个问题上发布了这个问题的答案: java - 如何检查日历实例是否最初是错误的日期

I posted the answer to this question on my other question: java - How to check if a Calendar instance was originally a wrong date.

原则是使用JodaTime API和CustomTime对象的自定义适配器和自定义包装器类型。

The principle is to use JodaTime API and a custom Adapter and a custom wrapper type for the DateTime object.

这篇关于检查xsd datetime在转换为Java对象之前是否有定义的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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