I have a variable containing the days since the epoch reference date of 1970-01-01 for a certain date.
Does someone know the way to convert this variable to a java.util.Calendar?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The following should work:
A note regarding time zones:
A new
GregorianCalendarinstance 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:This prints
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 theGregorianCalendar:new GregorianCalendar(TimeZone.getTimeZone("GMT")). If the calendar is created such, the output is:Now the calendar returns useful values. The reason why the
Datereturned byc.getTime()is still “off” is that thetoString()method uses the defaultTimeZoneto build the string. At the top we set this to GMT-1 so everything is normal.