从纪元以来的天数获取 java.util.Calendar

Get java.util.Calendar from days since epoch(从纪元以来的天数获取 java.util.Calendar)
本文介绍了从纪元以来的天数获取 java.util.Calendar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个变量,其中包含 纪元参考日期 code>1970-01-01 某个日期.

I have a variable containing the days since the epoch reference date of 1970-01-01 for a certain date.

有人知道如何将此变量转换为 java.util.Calendar?

Does someone know the way to convert this variable to a java.util.Calendar?

推荐答案

以下应该可以:

Calendar c = new GregorianCalendar();
c.setTime(new Date(0));

c.add(Calendar.DAY_OF_YEAR, 1000);

System.err.println(c.getTime());

<小时>

关于时区的说明:


A note regarding time zones:

使用运行程序的系统的默认时区创建一个新的 GregorianCalendar 实例.由于 Epoch 与 UTC(Java 中的 GMT)相关,因此必须小心处理任何不同于 UTC 的时区.下面的程序说明了这个问题:

A new GregorianCalendar instance is created using the default time zone of the system the program is running on. Since Epoch is relative to UTC (GMT in Java) any time zone different from UTC must be handled with care. The following program illustrates the problem:

TimeZone.setDefault(TimeZone.getTimeZone("GMT-1"));

Calendar c = new GregorianCalendar();
c.setTimeInMillis(0);

System.err.println(c.getTime());
System.err.println(c.get(Calendar.DAY_OF_YEAR));

c.add(Calendar.DAY_OF_YEAR, 1);
System.err.println(c.getTime());
System.err.println(c.get(Calendar.DAY_OF_YEAR));

打印出来

Wed Dec 31 23:00:00 GMT-01:00 1969
365
Thu Jan 01 23:00:00 GMT-01:00 1970
1

这表明仅使用 e.g. 是不够的.c.get(Calendar.DAY_OF_YEAR).在这种情况下,必须始终考虑到它是一天中的什么时间.这可以通过在创建 GregorianCalendar 时显式使用 GMT 来避免:new GregorianCalendar(TimeZone.getTimeZone("GMT")).如果日历是这样创建的,则输出为:

This demonstrates that it is not enough to use e.g. c.get(Calendar.DAY_OF_YEAR). In this case one must always take into account what time of day it is. This can be avoided by using GMT explicitly when creating the GregorianCalendar: new GregorianCalendar(TimeZone.getTimeZone("GMT")). If the calendar is created such, the output is:

Wed Dec 31 23:00:00 GMT-01:00 1969
1
Thu Jan 01 23:00:00 GMT-01:00 1970
2

现在日历返回有用的值.c.getTime() 返回的 Date 仍然关闭"的原因是 toString() 方法使用了默认的 TimeZone 来构建字符串.在顶部,我们将其设置为 GMT-1,因此一切正常.

Now the calendar returns useful values. The reason why the Date returned by c.getTime() is still "off" is that the toString() method uses the default TimeZone to build the string. At the top we set this to GMT-1 so everything is normal.

这篇关于从纪元以来的天数获取 java.util.Calendar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)