4

I have an input "+04:00"

What API can i use to get the timezone from this?


So far, 1) I looked into joda - https://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html

   static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) 

Not sure if this is ideal.

2) I checked Timezone class.

http://docs.oracle.com/javase/6/docs/api/java/util/TimeZone.html#getTimeZone%28java.lang.String%29

 getTimeZone

public static TimeZone getTimeZone(String ID)

    Gets the TimeZone for the given ID.

    Parameters:
        ID - the ID for a TimeZone, either an abbreviation such as "PST", a full name such as "America/Los_Angeles", or a custom ID such as "GMT-8:00". Note that the support of abbreviations is for JDK 1.1.x compatibility only and full names should be used. 
    Returns:
        the specified TimeZone, or the GMT zone if the given ID cannot be understood.

Can someone please explain how to give custom IDs as input to this function?

JodaStephen
  • 60,927
  • 15
  • 95
  • 117
webExplorer
  • 1,025
  • 2
  • 17
  • 33

1 Answers1

5

http://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html

There is a method in the TimeZone class called getTimeZone(String id) where you pass in a String, which can be like "GMT-04:00". You should check that out.

Just be sure you try out a few examples and make sure you are properly using it. Some of these default methods require specific formats.

StoneMan
  • 423
  • 3
  • 18
  • @webExplorer So the when you call the `getTimeZone(String id)` method, you get a TimeZone object. With this object, you can call the `getDisplayName(Boolean.TRUE, 0)` method. Now, the first input is a boolean which specifies whether daylight savings is present or not. The second input is whether the display name should be short (like PST, use 0) or long (Pacific Standard Time, use 1). Check out the API in my original answer, more details will be there. – StoneMan Jul 28 '14 at 23:29
  • Thanks for the answer.. I already checked that.. but I am not sure what ID to pass for the getTimeZone function. If you observe, whatever value i pass to the function it returns me GMT. – webExplorer Jul 28 '14 at 23:31
  • @webExplorer Ah I see. I really don't know beyond that, but I found a similar thread, not sure if you've already looked through this: http://stackoverflow.com/questions/4869206/converting-the-america-los-angeles-time-zone-to-pst-or-pdt-in-java-me – StoneMan Jul 28 '14 at 23:33