简体   繁体   中英

Java:Convert date in UTC to local time zone

I have date which is stored in db in UTC timezone. have to convert this date to systems local timezone. I'd looked into all other questions in stackoverflow but non of them works for me. Please can any one help me with this. I need output in java.utils.Date only as startAt() method of quartz scheduler accepts Only Date.

tl;dr

You do not need to apply a time zone.

Details

A Date already has a time zone: UTC. It represents a specific moment, a point on the timeline. You would only apply a time zone for display to user in the user's preferred wall-clock time . An alarm set for 6 AM UTC fires at 7 AM in Europe/Paris and 11:30 AM in Asia/Kolkata — the very same simultaneous moment.

Date is UTC

The java.util.Date class already uses UTC . You cannot adjust that object to another time zone. That class is simply a wrapper around a count of milliseconds since the first moment of 1970 in UTC. Do not be distracted by the confusing behavior of its toString method that applies the JVM's current default time zone in rendering the String; the internal value is in UTC .

So what you ask ( start with a Date , adjust to a local time zone, and render a Date ) is impossible and makes no sense.

I suggest you do some searching and studying of other date-time questions on StackOverflow to learn the concepts. Search specifically for these Java classes: Instant , ZoneId , ZonedDateTime .

Conversion

You can convert to java.time when needing to interface with old code not yet updated to the java.time types. Call new conversion methods added to the old classes.

Instant instant = myJavaUtilDate.toInstant();  // UTC.
ZonedDateTime zdt = instant.atZone( ZoneId.of( "America/Montreal" ) );  // Same moment, but different wall-clock time.

And you can convert from java.time classes.

LocalDate ld = LocalDate.of( 2016 , 1 , 23 );  // 2016-01-23. No time zone at all.
LocalTime lt = LocalTime.of( 6 , 0 , 0 );  // 06:00:00. No time zone at all.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime( ld , lt , z );
Instant instant = zdt.toInstant();  // UTC.

// Convert from modern classes to legacy class.
java.util.Date d = java.util.Date.from( instant );

The java.util.Date actually does have a time zone set down in its bowels. But that zone has no setter and no getter method. For our purposes here, we can ignore it. Confusing? Yes. One of many poor design choices made in the early Java date-time classes. And one of many reasons to avoid these classes. Stick with java.time classes only.

you can use the following code

    DateFormat localFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    localFormat.setTimeZone(TimeZone.getTimeZone("PST"));


    Date locaTime = new Date(localFormat.format(date));

I am using LocalDateTime from java 8. I use below code to get the local date time from unix timestamp. There might be other methods to get it from the format you want.

Date.from(LocalDateTime.ofEpochSecond(unixTimestamp, 0, ZoneOffset.UTC).toLocalDate());

通过研究StackOverFlows问题,可以找到石英类的DateBuilder,它有助于转换时区

Date startDate = DateBuilder.translateTime(job.getStartDate(), TimeZone.getTimeZone("UTC"), TimeZone.getDefault());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM