Set Time Zone of a java.util.Date
ZonedDateTimeto Set Time Zone of a
UTC Instantto Set Time Zone of a
This quick tutorial will look at how to use Java to set the time zone associated with a date. The
java.util.Date has no time zone.
It denotes the UTC/GMT standard with no time zone offset. Because when creating a string representation, its
toString function uses the JVM’s default time zone as the basis for its application.
java.text.SimpleDateFormat classes are generally difficult to work with and should be avoided. We’ll use the
java.time package that comes standard with Java 8 instead.
Below are the ways the
java.time classes can represent a time zone.
ZonedDateTime to Set Time Zone of a
Applying a time zone, an offset, and the guidelines for dealing with irregularities like
Daylight Saving Time or
DST is a better option than the alternative.
ZoneId is used with an
ZonedDateTime is produced. Always be sure to use the correct name for the time zone.
Abbreviations of three to four letters, such as
EST that are neither specific nor standardized should never be used.
ZoneId zId = ZoneId.of( "Pakistan/Karachi" ); ZonedDateTime zonedt = ZonedDateTime.ofInstant( instant , zId );
UTC Instant to Set Time Zone of a
Instant is the fundamental building component of
java.time. It represents an
instant on the timeline that is measured in
Implement the majority of your application logic using
Instant instant = Instant.now();
When any text being parsed does not include any indicators of offset or zone, then it should be interpreted as a
If you are definite about the time zone that should be used, you may generate a
ZonedDateTime by assigning a
ZoneId. See the following sample of code.
LocalDateTime.parse( "2022-09-10T01:13:15.123456789" ) .atZone( ZoneId.of( "Asia/Pakistan" ) )
Related Article - Java Date
- Find the Day of the Week Using Zellers Congruence in Java
- Java Get Day of Week
- Java Date vs. LocalDate
- Calendar Date in YYYY-MM-DD Format in Java
- Convert String to LocalDate in Java