Showing posts with label TimeZone. Show all posts
Showing posts with label TimeZone. Show all posts

getDisplayName - How to Get the Standard Time Zone, Short Form

public final String getDisplayName (boolean daylightTime, int style) was added in API level 1
Displays the Short Form the of the Standard Time Zone for the user's preferred time zone.
Equivalent to getDisplayName(daylightTime, style, Locale.getDefault()). Be wary of the default locale.

1. Initalize TimeZone object with the User's Preferred Time Zone with a TimeZone object named myTimeZone.

2. In the onCreate method, add a Toast, which displays myTimeZone.getDisplayName(false, TimeZone.SHORT)  

Toast.makeText(getBaseContext(), myTimeZone.getDisplayName(false, TimeZone.SHORT), Toast.LENGTH_LONG).show(); 

3. Compile and run!

4. For me, this displayed: "CST"
which stands for Central Standard Time

Resources:
http://developer.android.com/reference/java/util/TimeZone.html#getDisplayName(boolean, int)

getRawOffset - How to Get the Offset in Milliseconds from UTC

public abstract int getRawOffset () was added in API level 1
Returns the offset in milliseconds from UTC of this time zone's standard time.
Try to use Calendar and SimpleDateFormat instead, when you can. Let classes like Calendar and SimpleDateFormat do the date computations for you.

More usefully, the getOffset(int, int, int, int, int, int) methods return the actual offset from UTC for a given point in time; this is the raw offset plus (if the point in time is in daylight time) the applicable DST savings(usually, but not necessarily, 1 hour).

1. Initalize TimeZone object with the User's Preferred Time Zone with a TimeZone object named myTimeZone.

2. To display the TimeZone Offset, you can display a Toast, such as below. It returns an integer so the String.valueOf() must be used to display to a toast. To save it to an integer variable, the String.valueOf() is not needed. 

Toast.makeText(getBaseContext(),String.valueOf(myTimeZone.getRawOffset()), Toast.LENGTH_LONG).show();

3. Compile and run!

4. For me, in Central Standard Time, I get the below results displayed.

-21600000

5. This means I am located:

21,600,000 milliseconds from the UTC
1 minute= 60,000 milliseconds
21,600,000/60,000 = 360 minutes (-360 minutes)
60 minutes = 1 hour
360/60 = 6 hours (-6 hours)

So, my Time Zone is 6 hours less than UTC. 

getDisplayName() - How to get the Standard Time Zone, Long Form

public final String getDisplayName () was added in API level 1
Returns the standard time, in long form, of the user's preferred locale. Be wary of the default locale.

1. Initalize TimeZone object with the User's Preferred Time Zone with a TimeZone object named myTimeZone.

2. For an example, you can display the getDisplayName in a toast. Such as below. 

Toast.makeText(getBaseContext(), myTimeZone.getDisplayName(), Toast.LENGTH_LONG).show(); 

3. Compile and run!

4. For me, this displayed: "Central Standard Time"

Resources:
http://developer.android.com/reference/java/util/TimeZone.html#getDisplayName()

getDefault() - How to Initalize TimeZone object with the User's Preferred Time Zone

public static synchronized TimeZone getDefault () was added in API level 1
Returns the user's preferred time zone. This may have been overridden for this process with setDefault(TimeZone). Since the user's time zone changes dynamically, avoid caching this value. Instead, use this method to look it up for each use.

1. Declare TimeZone object
2. Add the line below to get the user's preferred time zone and save it to the TimeZime object named myTimeZone.
myTimeZone = TimeZone.getDefault();

3. Compile and run!

Next Recommended Article: getDisplayName() - How to get the Standard Time Zone, Long Form

Resources:
developer.android.com/reference/java/util/TimeZone.html#getDefault()

How to Declare TimeZone object

TimeZone was added in API Level 1
TimeZone represents a time zone, primarily used for configuring a Calendar or SimpleDateFormat instance.
Most applications will use getDefault() which returns a TimeZone based on the time zone where the program is running. You can also get a specific TimeZone by Olson ID.
It is highly unlikely you'll ever want to use anything but the factory methods yourself. Let classes like Calendar and SimpleDateFormat do the date computations for you.

1. Add the below import to the imports section.

import java.util.TimeZone;

2. Add the below line to the onCreate method. This will create a new TimeZone object named myTimeZone.

TimeZone myTimeZone;

3. Compile and run!
Resources:
developer.android.com/reference/java/util/TimeZone.html