简体   繁体   English

如何将Sharepoint Date从Hex解析为java.util.Date

[英]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. 我正在尝试将MS Sharepoint中的Date解析为java.util.Date。 The Details: I query a Sharepoint from a Grails webapp via the SOAP GetListItems method. 详细信息:我通过SOAP GetListItems方法从Grails Web应用程序查询一个Sharepoint。 In the Sharepoint list, the date is displayed correctly, here 09/11/2009 but in the SOAP response, I get 在Sharepoint列表中,正确显示了日期,这里是09/11/2009,但是在SOAP响应中,我得到了

0x01ca60cf|0x94894000
Further, this only happens with docx, pptx, etc. file types. 此外,这仅在docx,pptx等文件类型下发生。

So, does anyone know how to convert this into a java.util.Date? 那么,有谁知道如何将其转换为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. 我已经尝试过将两个十六进制值转换为Long或字节并进行移位,但是我用Google搜索的所有算法仅适用于提供的示例十六进制值。

[Edit] For example this SO solution (converted to Java) didn't work for my values. [编辑]例如, 这种 SO解决方案(转换为Java)不适用于我的价值观。

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: 啊...我不确定的唯一原因是为什么我需要这样做/ 10000 ,但是DateTime对象滴答声文档显示:

A single tick represents one hundred nanoseconds or one ten-millionth of a second. 一个滴答声代表一百纳秒或一百万分之一秒。 There are 10,000 ticks in a millisecond. 毫秒内有10,000个滴答声。

Which explains it :-) 解释它:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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