如何使用Axis将Java Date传递给.Net webservice

时间:2011-04-11 00:24:37

标签: java .net datetime calendar axis

我正在尝试从Java调用.Net Web服务。我有一个java.sql.Date,我转换为一个日历,然后作为DateTime传递给.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中,以下内容让我获得“AUS东部标准时间”:

TimeZone.CurrentTimeZone.StandardName;

据我所知,Java和.Net在同一时区都有本地时间......

2 个答案:

答案 0 :(得分:1)

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

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 webservices似乎是这样的互动:

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

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

我不知道是否是这种情况,并希望得到更好的解释...但是现在,除非另有说明,否则此解决方案似乎有效......

答案 1 :(得分:0)

根据那篇文章,.net总是处理当地时区的日期(哇,那就是破了)。因此,您必须确定.net服务的时区并在您的日历实例上设置该时区。