Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How can I create a TimeZone object for Mountain time?

Tags:

java

timezone

It is necessary that Daylight Savings is not disabled.

like image 536
700 Software Avatar asked Jan 20 '11 22:01

700 Software


People also ask

How do I set the TimeZone in Java?

You can make use of the following DateFormat. SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate.

What is UTC TimeZone in Java?

UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset.

How does Java handle different time zones?

If you cannot change the OS or the JVM timezone, you can still convert a Java Date/Time or Timestamp to a specific time zone using the following two JDBC methods: PreparedStatement#setTimestamp(int parameterIndex, Timestamp x, Calendar cal) – to convert the timestamp that goes to the database.

Does Java calendar have TimeZone?

The setTimeZone(TimeZone time_zone) method in Calendar class takes a Time Zone value as a parameter and modifies or set the timezone represented by this Calendar.


2 Answers

Well, in this list of zoneinfo time zone names there are plenty which claim to be "Mountain Time". Find the one that best fits what you want, and use that. For example:

TimeZone zone = TimeZone.getTimeZone("America/Denver");
like image 94
Jon Skeet Avatar answered Oct 21 '22 12:10

Jon Skeet


java.time

The java.util Date-Time API is outdated and error-prone. It is recommended to stop using it completely and switch to the modern Date-Time API*.

Use ZoneId#of instead of Timezone#getTimeZone e.g.

ZoneId zoneId = ZoneId.of("America/Denver");

AN ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Note: Check Mountain Time Zone and List of tz database time zones and choose a timezone ID that meets your requirement.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

like image 1
Arvind Kumar Avinash Avatar answered Oct 21 '22 12:10

Arvind Kumar Avinash