java Long 到 XMLGregorianCalendar 并返回到 Long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12867130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:34:04  来源:igfitidea点击:

Long To XMLGregorianCalendar and back to Long

javadatejaxb

提问by JD Frias

I am trying to convert from millisecond time stamp to XMLGregorianCalendar and back, but I seem to be getting wrong results. Am I doing something wrong? It seems I am gaining days.

我正在尝试从毫秒时间戳转换为 XMLGregorianCalendar 并返回,但我似乎得到了错误的结果。难道我做错了什么?看来我的日子越来越好。

    // Time stamp   01-Jan-0001 00:00:00.000
    Long ts = -62135740800000L;
    System.out.println(ts);
    System.out.println(new Date(ts)); // Sat Jan 01 00:00:00 PST 1 .. Cool!

    // to Gregorian Calendar
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTimeInMillis(ts);

    // to XML Gregorian Calendar
    XMLGregorianCalendar xc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);

    // back to GC
    GregorianCalendar gc2 = xc.toGregorianCalendar();

    // to Timestamp
    Long newTs = gc2.getTimeInMillis();
    System.out.println(newTs);    //  -62135568000000  .. uh?
    System.out.println(new Date(newTs));  // Mon Jan 03 00:00:00 PST 1  .. where did the extra days come from?

回答by DNA

Interesting - it works fine for values down to (about) -10000000000000L (and positive values) but larger negative values become inconsistent.

有趣 - 它适用于低至(约)-10000000000000L(和正值)的值,但较大的负值变得不一致。

If you print out gc, xc, and gc2, you can see where the problem arises (the conversion from XMLGregorianCalendar to GregorianCalendar

如果打印出gc, xc, 和gc2,您可以看到问题出在哪里(从 XMLGregorianCalendar 到 GregorianCalendar 的转换

gc:  java.util.GregorianCalendar[time=-62135740800000 ... DAY_OF_WEEK=7
xc:  0001-01-01T08:00:00.000Z
gc2: java.util.GregorianCalendar[time=? ... DAY_OF_WEEK=5

If you print out the fields of xc, you get 1,1,1.

如果打印出 的字段xc,则会得到 1,1,1。

    System.out.println(xc.getYear());
    System.out.println(xc.getMonth());
    System.out.println(xc.getDay());

For gc2, you get 1,0,1 (which matches xc, because months are zero-based in GregorianCalendar)

对于gc2,您会得到 1,0,1(匹配xc,因为月份在 GregorianCalendar 中是从零开始的)

    System.out.println(gc2.get(gc2.YEAR));
    System.out.println(gc2.get(gc2.MONTH));
    System.out.println(gc2.get(gc2.DAY_OF_MONTH));

However, adding these 3 printlncalls changes the output from printing out gc2! - the time=?output from gc2changes to time=-62135568000000- so some calculation has been triggered by querying the GregorianCalendarobject; the areFieldsSetproperty also changes from falseto true.

但是,添加这 3 个println调用会更改打印输出gc2!-time=?输出gc2更改为time=-62135568000000- 因此通过查询GregorianCalendar对象触发了一些计算;该areFieldsSet属性也从false变为true

The timezones of the two GregorianCalendars are different, but this does not account for the error, which persists even if you set explicit TimeZone and Locale.

两个 GregorianCalendar 的时区不同,但这并不能解释错误,即使您设置了明确的 TimeZone 和 Locale,该错误仍然存​​在。

回答by Yogendra Singh

I believe here is the problem. Per documentation, toGregorianCalendar()relies on the GregorianCalendarcorresponding defaults for conversion when there is any field missing.

我相信这是问题所在。根据文档,当缺少任何字段时,toGregorianCalendar()依赖于GregorianCalendar相应的默认值进行转换。

If you try:

如果你试试:

Date date = new Date();
long ts = date.getTime(); //in place of your input

and run your code, you should find, both toand fromconversion working fine.

并运行您的代码,您应该会发现tofrom转换工作正常。

If you want your toGregorianCalendar()with custom provide inputs as in your example, please use toGregorianCalendar(TimeZone,Locale,Defaults)and supply the updated defaults to be used in conversion.

如果您希望toGregorianCalendar()在您的示例中使用自定义提供输入,请使用toGregorianCalendar(TimeZone,Locale,Defaults)并提供用于转换的更新默认值。