如何使用 Axis 将 Java 日期传递到 .Net Web 服务
我正在尝试从 Java 调用 .Net Web 服务。我有一个 java.sql.Date,我正在将其转换为日历,然后将其作为日期时间传递到 .Net。
不幸的是,当它到达另一边时,它比发送日期晚了一天。这是一个已知问题(http://wiki.apache.org/ws/FrontPage/Axis/DotNetInterop),我确信有一种解决方法,但我似乎找不到它。
有谁知道如何正确地将 java.sql.Date 转换为日历,这样就不存在 24 小时偏移问题?
我现在的代码如下:
java.sql.Date myDate = Date.valueOf("2011-04-11");
Calendar calendarDate = Calendar.getInstance();
calendarDate.clear();
calendarDate.setTime(myDate); //we then pass calendarDate off to webservice...
当我查看时区信息时,我看到以下内容:
在 Java 中,以下内容让我“东部标准时间(新南威尔士)”:
calendarDate.getTimeZone().getDisplayName();
在 .Net 中,以下内容让我“澳大利亚东部”标准时间”:
TimeZone.CurrentTimeZone.StandardName;
据我目前所知,Java 和 .Net 的本地时间都位于同一时区......
I'm trying to call a .Net webservice from Java. I have a java.sql.Date that I am converting to a Calendar which then gets passed through to .Net as a DateTime.
Unfortunately, when it gets to the other side it is a day behind the date that was sent. This is a known issue as per (http://wiki.apache.org/ws/FrontPage/Axis/DotNetInterop) and I'm sure there is a way around it but I just can't seem to find it.
Does anyone know of a way to correctly convert a java.sql.Date to a Calendar so that there is no 24 hour offset issue?
The code I have at the moment is as follows:
java.sql.Date myDate = Date.valueOf("2011-04-11");
Calendar calendarDate = Calendar.getInstance();
calendarDate.clear();
calendarDate.setTime(myDate); //we then pass calendarDate off to webservice...
When I look at the timezone info I see the following:
In Java the following gets me "Eastern Standard Time (New South Wales)":
calendarDate.getTimeZone().getDisplayName();
In .Net the following gets me "AUS Eastern Standard Time":
TimeZone.CurrentTimeZone.StandardName;
As far as I am currently aware, both Java and .Net have the local time in the same timezone...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否是正确的做法...但它似乎解决了我的问题...
将偏移量设置为零似乎可以完全避免偏移问题。
我认为这是可行的,因为 Java 和 .Net Web 服务似乎是这样交互的:
我认为我在设置日期后将偏移量设置为零的修复会导致日历在没有偏移量的情况下保持日期与当地时间一致。因此,当日期到达 .Net Web 服务时,当地时间的假设是正确的。
我不知道情况是否如此,希望有更好的解释......但就目前而言,除非另有说明,这个解决方案似乎有效......
I'm not sure if this is the correct thing to do...but it seems to have fixed my problem...
Setting the offset to zero seems to allow it to avoid the offset issue altogether.
I think this works because the Java and .Net webservices seem to interact like this:
I think my fix of setting the offset to zero after setting the date causes the Calendar to keep the date consistent with local time in the absence of the offset. Thus when the date gets to the .Net webservice the assumption of local time is correct.
I have no idea if that is the case or not and would appreciate a better explanation...but for now, unless told otherwise, this solution appears to work...
根据那篇文章,.net 总是处理本地时区的日期(哇,那是不是坏了)。因此,您必须确定 .net 服务的时区并在您的日历实例上设置该时区。
according to that article, .net always handles dates in the local timezone (wow, is that broken). so, you must determine the timezone of the .net service and set that timezone on your Calendar instance.