views:

52

answers:

1

Hello,

I'm trying to parse a Date from a MS Sharepoint into a java.util.Date. The Details: I query a Sharepoint from a Grails webapp via the SOAP GetListItems method. In the Sharepoint list, the date is displayed correctly, here 09/11/2009 but in the SOAP response, I get

0x01ca60cf|0x94894000
Further, this only happens with docx, pptx, etc. file types.

So, does anyone know how to convert this into a java.util.Date? I already tried converting the two hex values to a Long or to bytes and shifting them around, but all algorithms I googled only work for the supplied sample hex values.

[Edit] For example this SO solution (converted to Java) didn't work for my values.

+3  A: 

After a bit of educated trial and error, I ended up with this:

def date = "0x01ca60cf|0x94894000"

// Parse our hex numbers into a single number
def nums = Long.parseLong( date.split( /\|/ ).collect { it.replace( '0x', '' ) }.join( '' ), 16 ) / 10000
// MS calendar goes from 1600...  Java's goes from 1970, so we need to make up the difference
nums += Calendar.instance.updated( year:1601, month:0, date:1 ).time.time

println "Converted date is ${new Date( nums as Long )}"

You would probably want to do much more testing to make sure it isn't just a fluke I get the right date on this occasion...

Do you have more values to test it on?

EDIT...

Ahhh...the only bit I wasn't sure about was why I needed to do / 10000, but the documentation for ticks in the DateTime object shows that:

A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.

Which explains it :-)

tim_yates
It works! I tested it on several values. Thank you very much!
air_blob
Cool :-) The only bit I can't explain is the `/ 10000`...but if it works, it works ;-)
tim_yates
Solved the `/ 10000` quandry, and put why in the answer
tim_yates