-1

I am trying to convert 2015-01-04T19:50:26+08:00 to local time("Australia/Melbourne") in Java.May I know what are the libraries I can use for this??

navrani
  • 157
  • 1
  • 1
  • 7
  • Take a look at these, it may help: [Date TimeZone conversion in java?](https://stackoverflow.com/questions/5422089/date-timezone-conversion-in-java) [Java Convert GMT/UTC to Local time doesn't work as expected](https://stackoverflow.com/questions/19375357/java-convert-gmt-utc-to-local-time-doesnt-work-as-expected) – Sarah Aziziyan Aug 27 '17 at 12:02
  • HI @Sarah, Yes that helps. But can you explain me this bit? 2015-01-04T19:50:26+08:00 is GMT time. Isn't it? – navrani Aug 27 '17 at 12:23
  • The Answer is here. https://stackoverflow.com/questions/5422089/date-timezone-conversion-in-java – navrani Aug 27 '17 at 12:32
  • @navrani and HAYMbl4, I discourage using the answers found in the question both of you are linking to. Most of them use the long outdated `SimpleDateFormat` class, while the least poor of them uses Joda-Time, which is also being discontinued. – Ole V.V. Aug 28 '17 at 10:01
  • The modern Java date & time API, AKA as `java.time` or JSR-310, is very well suited for tasks like yours. [Tutorial here](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 28 '17 at 10:04
  • Excuse my frankness, what did you try before posting your question? I am sure your search engine could help you faster and better than anyone can post an answer here. – Ole V.V. Aug 28 '17 at 10:06

1 Answers1

0

You could do it simply adding or subtracting hours following this table:

|Western Standard Time|>. add 8 hours to UTC|

|Western Summer Time|>. add 9 hours to UTC|

|Central Standard Time|>. add 9:30 hours to UTC|

|Central Summer Time|>. add 10:30 hours to UTC|

|Eastern Time|>. add 10 hours to UTC|

|Eastern Summer Time|>. add 11 hours to UTC|

Table from earthsky.

I would do it declaring a dictionary with name of location as key and hours as value. Negative values mean substract.

Hope it helps you.

  • While that would work, it doesn’t take into account that offsets have historically changed and may change again in the future. Since Java installations come with time zone information, it’s generally better to use the built-in classes and methods that use this time zone information. I specifically recommend `java.time.OffsetDateTime`, `ZoneId` and `ZonedDateTime`. – Ole V.V. Aug 28 '17 at 09:58