I have a timestamp that's in UTC and I want to convert it to local time without using an API call like TimeZone.getTimeZone("PST")
. How exactly are you supposed to do this? I've been using the following code without much success:
private static final SimpleDateFormat mSegmentStartTimeFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(mSegmentStartTimeFormatter.parse(startTime));
}
catch (ParseException e) {
e.printStackTrace();
}
return calendar.getTimeInMillis();
Sample input value: [2012-08-15T22:56:02.038Z]
should return the equivalent of [2012-08-15T15:56:02.038Z]
Date
has no timezone and internally stores in UTC. Only when a date is formatted is the timezone correction applies. When using a DateFormat
, it defaults to the timezone of the JVM it's running in. Use setTimeZone
to change it as necessary.
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = utcFormat.parse("2012-08-15T22:56:02.038Z");
DateFormat pstFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
pstFormat.setTimeZone(TimeZone.getTimeZone("PST"));
System.out.println(pstFormat.format(date));
This prints 2012-08-15T15:56:02.038
Note that I left out the 'Z'
in the PST format as it indicates UTC. If you just went with Z
then the output would be 2012-08-15T15:56:02.038-0700
Use the modern Java date & time API, and this is straightforward:
String inputValue = "2012-08-15T22:56:02.038Z";
Instant timestamp = Instant.parse(inputValue);
ZonedDateTime losAngelesTime = timestamp.atZone(ZoneId.of("America/Los_Angeles"));
System.out.println(losAngelesTime);
This prints
2012-08-15T15:56:02.038-07:00[America/Los_Angeles]
Points to note:
Z
in your timestamp means UTC, also known as Zulu time. So in your local time value, the Z
should not be there. Rather you would want a return value like for example 2012-08-15T15:56:02.038-07:00
, since the offset is now -7 hours rather than Z.Instant
objects. Convert to ZonedDateTime
only when you have a need, like for presentation.Question: Can I use the modern API with my Java version?
If using at least Java 6 , you can.
Here is a Simple Modified solution
public String convertToCurrentTimeZone(String Date) {
String converted_date = "";
try {
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = utcFormat.parse(Date);
DateFormat currentTFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
currentTFormat.setTimeZone(TimeZone.getTimeZone(getCurrentTimeZone()));
converted_date = currentTFormat.format(date);
}catch (Exception e){ e.printStackTrace();}
return converted_date;
}
//get the current time zone
public String getCurrentTimeZone(){
TimeZone tz = Calendar.getInstance().getTimeZone();
System.out.println(tz.getDisplayName());
return tz.getID();
}
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.