views:

56

answers:

2

Hi All,

I am having some issues with a Java web service i have consumed into my simple outlook add in that i am trying to integrate with this 3rd party API. The problem is the difference between .Net dates and Java ones, i will be honest i do not know much about Java anymore. My problem is that the API is expecting a date in the format yyyyMMddHHmmss which is fine, but when i try and create that using something like DateTime.ParseExact i recieve an error stating that "String was not recognized as a valid DateTime." Not really sure what my options our as Google has not really helped me out this time.

A: 

On the .NET side, you should be able to do something like this:

// convert DateTime to string
return DateTime.Now.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture);

// convert string to DateTime
if(value.Length != 14)
    throw new ArgumentException("Value must be 14 characters.");
int year = Int32.Parse(value.Substring(0, 4));
int month = Int32.Parse(value.Substring(4,2));
int day = Int32.Parse(value.Substring(6,2));
int hour = Int32.Parse(value.Substring(8,2));
int minute = Int32.Parse(value.Substring(10,2));
int second = Int32.Parse(value.Substring(12,2));
return new DateTime(year, month, day, hour, minute, second, 0);

The manual string parsing with substrings is a bit hoakey, but it technically works. Hope it helps!

rally25rs
A: 

I think was doing something wrong, i wasnt formatting the date in the correct format before using parse exact:

        string fd = new DateTime(2010, 07, 10, 01, 00, 00).ToString();
        string sd = new DateTime(2010, 07, 10, 23, 59, 59).ToString();

        fd = String.Format("{0:yyyyMMddHHmmss}", DateTime.Parse(fd));
        sd = String.Format("{0:yyyyMMddHHmmss}", DateTime.Parse(sd));

        api.searchMessage(null,
             null,
             null,
             "nosort",
             "archivedate",
             DateTime.ParseExact(fd, "yyyyMMddHHmmss", null),
             DateTime.ParseExact(sd, "yyyyMMddHHmmss", null),
             100);

Not sure if it will return data, but at least it is not erroring :)

Rob

Modika
That looks like you are passing a date object to the api method, not a string in the format yyyyMMddHHmmss... If the API is fine with that then this is good. In fact generally you seem to be doing a lot of unnecessary conversions from date to string and back again... That may be test code but if its final code its a bit of a mess...
Chris
yeah its test code, just trying to get my head around passing the date around, and this is really a proof of concept. When consumed the API is expecting a DateTime not a string but the API documentation (what little of it there is) is telling me to pass it in that format.
Modika