34

What is the simplest way of getting a Calendar object from a java.time.Instant or java.time.ZonedDateTime?

6
  • 2
    Judging 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, 2015 at 7:22
  • 3
    And probably you also tried - GregorianCalendar.from(zonedDateTime);
    – Rohit Jain
    Feb 28, 2015 at 7: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. Feb 28, 2015 at 7: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. Feb 28, 2015 at 7: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. Feb 28, 2015 at 7:55

3 Answers 3

43

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.

1
  • 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. May 29, 2019 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()));
1

How about GregorianCalendar.from(ZonedDateTime.ofInstant(instant, zoneId))?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.