简体   繁体   中英

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. Two of the parameters are of type Calendar. I'm having great difficulty in trying to pass these two dates to the web service.

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

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#?

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.

A word of warning: the SimpleDateFormat java class is not threadsafe. You are much better off either using

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

Then you will simply call the print and parse methods.

If you are using some kind of data-binding method, please mention it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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