简体   繁体   English

C#使用Calendar参数调用Java Web服务

[英]C# calling a Java web service with a Calendar parameter

I'm writing a C# desktop client that needs to call a web service written in Java. 我正在编写一个C#桌面客户端,该客户端需要调用用Java编写的Web服务。 Two of the parameters are of type Calendar. 其中两个参数的类型为Calendar。 I'm having great difficulty in trying to pass these two dates to the web service. 我很难将这两个日期传递给Web服务。

I've tried the following ways, all without success. 我尝试了以下方法,但都没有成功。

DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddDays(2);

DateTime startDate = new DateTime(2012, 3, 1, 1, 1, 1, DateTimeKind.Unspecified);
DateTime endDate = new DateTime(2012, 4, 1, 1, 1, 1, DateTimeKind.Unspecified);

DateTime startDate = new DateTime(2012, 3, 1, 1, 1, 1, DateTimeKind.Utc);
DateTime endDate = new DateTime(2012, 4, 1, 1, 1, 1, DateTimeKind.Utc);

DateTime startDate = new DateTime(2000, 1, 1, new System.Globalization.GregorianCalendar());
DateTime endDate = new DateTime(2012, 1, 1, new System.Globalization.GregorianCalendar());

I wrote a test Java client using the following code and this works... 我使用以下代码编写了一个测试Java客户端,并且可以正常工作...

GregorianCalendar calStartDate = new GregorianCalendar();
GregorianCalendar calEndDate = new GregorianCalendar();

calStartDate.set(2011, 5, 21);
calEndDate.set(2012, 5, 24);

XMLGregorianCalendar startDate = dtf.newXMLGregorianCalendar(calStartDate);
XMLGregorianCalendar endDate = dtf.newXMLGregorianCalendar(calEndDate);

Any suggests as to how I can pass a Calendar parameter from C#? 关于如何从C#传递Calendar参数有任何建议吗?

Thanks! 谢谢!

By far, the easiest and most portable thing to do here is turn a date into a string that includes the timezone, such as the ISO8601 format. 到目前为止,最简单,最可移植的操作是将日期转换为包含时区的字符串,例如ISO8601格式。

A word of warning: the SimpleDateFormat java class is not threadsafe. 一个警告:SimpleDateFormat Java类不是线程安全的。 You are much better off either using 您无论使用哪种方式都更好

http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html

or 要么

http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/time/FastDateFormat.html http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/time/FastDateFormat.html

Then you will simply call the print and parse methods. 然后,您只需调用print和parse方法。

If you are using some kind of data-binding method, please mention it. 如果您使用某种数据绑定方法,请提及。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM