2

How to convert the ZoneId to DateTimeZone? I have :

 Private Instant logTime;  //java.time
    Private ZoneId timeZoneId;   //java.time

But I need to get DateTime and DateTimeZone (both org.joda.time) from the above. I got DateTime from Instant like =

Private DateTime  dateTime ;   //org.joda.time
private DateTimeZone dateTimeZone;   //org.joda.time

LocalDateTime currentTime = new LocalDateTime(logTime);
dateTime = currentTime.toDateTime();

For dateTimeZone, I did :

dateTimeZone = DateTimeZone.forID(timeZoneId.getId()); // getting java.lang.NullPointerException

But getting error. Can someone tell mw how to do it properly

This gives me error. Can some one tell how should I convert this. I need to get the current time for the given zoneId.

  • A `ZoneId` doesn't have any date/time information. Did you mean get the current time in a specific zone id? – Tunaki Apr 26 '16 at 19:25
  • So I can get the current time (localTimezone) in a specific zone id ? –  Apr 26 '16 at 19:28
  • 2
    Could you [edit] your post with what you have and what you want then? Because it is unclear at the moment. – Tunaki Apr 26 '16 at 19:29
  • Are you trying to use `java.time` or jodatime or mix the two? – Misha Apr 26 '16 at 19:36
  • Something like this? http://stackoverflow.com/a/33100195/1743880 – Tunaki Apr 26 '16 at 19:40
  • 1
    There is no `ZoneId` class in jodatime. that's a class from `java.time`. Mixing the two APIs is going to cause a lot of confusion because many classes are named the same. For example, it's not clear if `private Instant logTime` is a `java.time.Instant` or `org.joda.time.Instant` – Misha Apr 26 '16 at 19:43
  • @Misha Updated the question –  Apr 26 '16 at 19:59
  • 1
    Update your question with the error that you are getting. Please read [ask] and [mcve] for guidelines. – Misha Apr 26 '16 at 20:00
  • @Learner Your question is not making sense. The java.time framework built into Java 8 and later is the official successor to the Joda-Time library. There should be no need to mix the two. – Basil Bourque Apr 26 '16 at 20:19
  • Thanks for now pointing out the NPE but you have still not given any information/data why your variable `timeZoneId` is null. Please check how you create this variable. Do you set it at all? – Meno Hochschild Apr 28 '16 at 07:19

1 Answers1

2

I faced the same time zone conversion issue and I could achieve the conversion by completing @basil-bourque answer on this post Converting from java.util.TimeZone to org.joda.DateTimeZone with a call to org.joda.time.DateTimeZone#forTimeZone(java.util.TimeZone zone) .

final java.time.ZoneId timeZoneId;
final org.joda.time.DateTimeZone timeZone = org.joda.time.DateTimeZone.forTimeZone(java.util.TimeZone.getTimeZone(timeZoneId))
Emmanuel Guiton
  • 1,315
  • 13
  • 24