It is necessary that Daylight Savings is not disabled.
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.
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.
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.
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.
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");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With