如何使用 Axis 将 Java 日期传递到 .Net Web 服务

发布于 2024-10-31 14:50:15 字数 792 浏览 1 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2024-11-07 14:50:15

我不确定这是否是正确的做法...但它似乎解决了我的问题...

java.sql.Date myDate = Date.valueOf("2011-04-11");
Calendar calendarDate = Calendar.getInstance();

//normalise the SQL date 
//http://download.oracle.com/javase/6/docs/api/java/sql/Date.html
calendarDate.set(Calendar.HOUR_OF_DAY, 0);
calendarDate.set(Calendar.MINUTE, 0);
calendarDate.set(Calendar.SECOND, 0);
calendarDate.set(Calendar.MILLISECOND, 0);

calendarDate.setTime(myDate);

calendarDate.set(Calendar.DST_OFFSET, 0); //Clear the daylight savings offset
calendarDate.set(Calendar.ZONE_OFFSET, 0); //Clear the timezone offset

将偏移量设置为零似乎可以完全避免偏移问题。

我认为这是可行的,因为 Java 和 .Net Web 服务似乎是这样交互的:

  • Java 日期是 GMT 加上偏移量
  • Axis 似乎将日期传递给 .Net,而没有偏移量信息。
  • 然后.Net 假定当地时间实际上是 GMT……这会导致 +/-24 小时的偏移问题。

我认为我在设置日期后将偏移量设置为零的修复会导致日历在没有偏移量的情况下保持日期与当地时间一致。因此,当日期到达 .Net Web 服务时,当地时间的假设是正确的。

我不知道情况是否如此,希望有更好的解释......但就目前而言,除非另有说明,这个解决方案似乎有效......

I'm not sure if this is the correct thing to do...but it seems to have fixed my problem...

java.sql.Date myDate = Date.valueOf("2011-04-11");
Calendar calendarDate = Calendar.getInstance();

//normalise the SQL date 
//http://download.oracle.com/javase/6/docs/api/java/sql/Date.html
calendarDate.set(Calendar.HOUR_OF_DAY, 0);
calendarDate.set(Calendar.MINUTE, 0);
calendarDate.set(Calendar.SECOND, 0);
calendarDate.set(Calendar.MILLISECOND, 0);

calendarDate.setTime(myDate);

calendarDate.set(Calendar.DST_OFFSET, 0); //Clear the daylight savings offset
calendarDate.set(Calendar.ZONE_OFFSET, 0); //Clear the timezone offset

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:

  • Java dates are GMT plus an offset
  • Axis seems to pass the date to .Net without the offset information.
  • .Net then assumes local time for a time that is actually GMT...which leads to offset problems of +/-24 hours.

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...

秋千易 2024-11-07 14:50:15

根据那篇文章,.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文