What is the simplest way of getting a Calendar
object from a java.time.Instant
or java.time.ZonedDateTime
?
Asked
Active
Viewed 2.5k times
33

JodaStephen
- 60,927
- 15
- 95
- 117

Daniel C. Sobral
- 295,120
- 86
- 501
- 681
-
2Judging by your reputation I suspect that you probably tested `Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(Instant.now().toEpochMilli());` so what is wrong with this solution? – Pshemo Feb 28 '15 at 07:22
-
3And probably you also tried - `GregorianCalendar.from(zonedDateTime);` – Rohit Jain Feb 28 '15 at 07:24
-
@RohitJain I haven't tried that -- I didn't even remember `GregorianCalendar`. That's exactly the kind of thing I was looking for! I can't accept comments, though. – Daniel C. Sobral Feb 28 '15 at 07:47
-
@Pshemo That is missing the zone. I need four lines to return a `Calendar` using that. I could one-line with the builder, but the line gets so long it's better broken off into four lines as well. Now, compared that, which I did use, to the solution that Rohit provided and you'll see what's wrong with it and why I asked. – Daniel C. Sobral Feb 28 '15 at 07:49
-
2@JBNizet 1) Yes. 2) I was not satisfied with the solution I came up with. 3) Javadoc is freely available, but finding a method going from `Instant` or `ZonedDateTime` to `Calendar` by looking at `GregorianCalendar` is not something crossed my mind. It's non-obvious and I didn't find it on a google search. All of which you might have realized if you had actually tried to answer the question instead of blithely, and incorrectly, assuming it was trivial. – Daniel C. Sobral Feb 28 '15 at 07:55
-
@DanielC.Sobral That is what I suspected. I added my comment just so others could read your response and learn about what exactly you didn't like in this approach (since you didn't mention it in your question). And yes, Rohit's solution seem to be perfect here. – Pshemo Feb 28 '15 at 13:23
3 Answers
42
Getting a Calendar
instant from ZonedDateTime
is pretty straight-forward, provided you know that there exists a GregorianCalendar#from(ZonedDateTime)
method. There was a discussion in Threeten-dev mail group, about why that method is not in Calendar
class. Not a very deep discussion though.
However, there is no direct way to convert from an Instant
to Calendar
. You've to have an intermediate state for that:
Instant instant = Instant.now();
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
Calendar cal1 = GregorianCalendar.from(zdt);
This is probably because, as evident from the table on this oracle tutorial, an Instant
maps to Date
rather than a Calendar
. Similarly, a ZonedDateTime
maps to a Calendar
.

Rohit Jain
- 209,639
- 45
- 409
- 525
-
Should be noted, that if original `Instant` is itself obtained from a `GregorianCalendar`, then original calendar and converted through the `ZonedDateTime` will not be equal (despite having same Epoch-Milli value) - because `GregorianCutover` date will very likely be different. – M. Prokhorov May 29 '19 at 10:22
4
You need to get the TimeZone using the instant and then you can get a calendar.
Calendar myCalendar = GregorianCalendar.from(ZonedDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));

Richard Barker
- 1,161
- 2
- 12
- 30
1
How about GregorianCalendar.from(ZonedDateTime.ofInstant(instant, zoneId))
?

Nils Breunese
- 1,169
- 2
- 8
- 12
-
Unable to obtain ZonedDateTime from TemporalAccessor: 2001-12-17T09:30:47Z of type java.time.Instant – Line Oct 25 '17 at 14:56
-
Indeed, you need to specify a ZoneId as well. I updated my answer. – Nils Breunese Oct 28 '17 at 16:01