private XMLGregorianCalendar Date2XMLDate(Date d)
throws DatatypeConfigurationException {
Calendar c = new GregorianCalendar();
c.setTime(d);
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DAY_OF_MONTH);
int year = c.get(Calendar.YEAR);
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar x = df.newXMLGregorianCalendarDate(year, month, day, c.getTimeZone().getOffset(d.getTime())/(60*60*1000));
return x;
}
Showing posts with label DateTime. Show all posts
Showing posts with label DateTime. Show all posts
Friday, February 29, 2008
Friday, February 8, 2008
Converting DateTime Object from .NET to XSD
if you are trying to consume a Java Web Service in .NET simply follow the instruction in Consuming a Web Service in .NET, you might run into some issues when dealing with DateTime Object. You want to make sure that the format of DateTime being sent to the Web Service(WS) is the one it expects. The WS i was working with required the DateTime in XSD format.
You can use XmlConvert or SoapDateTime class to convert the date to a string, but the problem is the the format they return is "2007-12-14T10:37:00.0000000-05:00" and the actual XSD format is "2007-12-14T10:37:00.000-05:00". There are 4 extra zeros. So i wrote a simple method to convert .NET DateTime Object to XSD format.
public String toXSDDateFormat(DateTime d)
{
String strD = XmlConvert.ToString(d);
String d1 = strD.Substring(0,strD.LastIndexOf(".")+4);
String d2 = strD.Substring(strD.LastIndexOf("-"),strD.Length -
strD.LastIndexOf("-"));
return d1+d2;
}
Hope this helps !!
You can use XmlConvert or SoapDateTime class to convert the date to a string, but the problem is the the format they return is "2007-12-14T10:37:00.0000000-05:00" and the actual XSD format is "2007-12-14T10:37:00.000-05:00". There are 4 extra zeros. So i wrote a simple method to convert .NET DateTime Object to XSD format.
public String toXSDDateFormat(DateTime d)
{
String strD = XmlConvert.ToString(d);
String d1 = strD.Substring(0,strD.LastIndexOf(".")+4);
String d2 = strD.Substring(strD.LastIndexOf("-"),strD.Length -
strD.LastIndexOf("-"));
return d1+d2;
}
Hope this helps !!
Subscribe to:
Posts (Atom)