如何使用Axis将Java日期传递给.NETWebService
我正在尝试从Java调用.Net Web服务。我有一个java.sql.Date,我正在将它转换为日历,然后作为日期时间传递到.Net 不幸的是,当它到达另一边时,它比发送日期晚了一天。这是一个众所周知的问题(http://wiki.apache.org/ws/FrontPage/Axis/DotNetInterop)我肯定有办法解决,但我就是找不到 有人知道如何正确地将java.sql.Date转换为日历,从而不存在24小时偏移问题吗 我现时的守则如下:如何使用Axis将Java日期传递给.NETWebService,java,.net,datetime,calendar,axis,Java,.net,Datetime,Calendar,Axis,我正在尝试从Java调用.Net Web服务。我有一个java.sql.Date,我正在将它转换为日历,然后作为日期时间传递到.Net 不幸的是,当它到达另一边时,它比发送日期晚了一天。这是一个众所周知的问题(http://wiki.apache.org/ws/FrontPage/Axis/DotNetInterop)我肯定有办法解决,但我就是找不到 有人知道如何正确地将java.sql.Date转换为日历,从而不存在24小时偏移问题吗 我现时的守则如下: java.sql.Date myDat
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中,下面是“东部标准时间(新南威尔士)”:
在.Net中,下面是“澳大利亚东部标准时间”:
据我目前所知,Java和.Net都在同一时区中有本地时间…根据那篇文章,.Net总是在本地时区中处理日期(哇,是不是坏了)。因此,您必须确定.net服务的时区,并在您的日历实例上设置该时区。我不确定这是否是正确的操作…但它似乎已经解决了我的问题
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和java中查看时区信息时得到的结果更新了我的答案。
TimeZone.CurrentTimeZone.StandardName;
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