Java: How can I create a TimeZone object for Mountain time?
It is necessa开发者_开发问答ry that Daylight Savings is not disabled.
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");
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.
精彩评论