简体   繁体   中英

How to parse a Sharepoint Date from Hex to a java.util.Date

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.

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 :-)

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