Reputation: 61
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.
Upvotes: 0
Views: 3715
Reputation: 61
By looking into StackOverFlows questions find a class DateBuilder of quartz which helps to translate the timezone
Date startDate = DateBuilder.translateTime(job.getStartDate(), TimeZone.getTimeZone("UTC"), TimeZone.getDefault());
Upvotes: 0
Reputation: 338171
You do not need to apply a time zone.
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 UTCThe 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
.
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.
Upvotes: 2
Reputation: 196
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());
Upvotes: 0
Reputation: 5871
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));
Upvotes: 0