As you already found out, you can already successfully parse your String
value into a Date
object:
DateFormat df = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.ROOT);
Date date = df.parse("Thu Dec 13 00:00:00 EST 2018");
But as a Date
object is an 'instant in time', it does not hold any time-zone information; it is nothing more than a container for milliseconds, which means after parsing the time-zone information gets lost. See Java API doc for Date
:
The class Date represents a specific instantin time, with millisecond precision
Fortunately, since Java 8 there is a new time-API (see package java.time
) that has many features that are missing in the old Date
or Calendar
API.
To parse your String
value into a full-featured date/time object including time zone, use a java.time.format.DateTimeFormatter
now:
DateTimeFormatter format = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.ROOT);
ZonedDateTime time = ZonedDateTime.parse("Thu Dec 13 00:00:00 EST 2018", dtf);
Now you can use this java.time.ZonedDateTime
directly, or convert it to any other time zone, preserving the "instant in time":
ZonedDateTime gmtTime = time.withZoneSameInstant(ZoneId.of("GMT");
If you are just interested in the local time (skipping time zone information), convert it to a LocalDateTime
:
LocalDateTime localTime = zdt.toLocalDateTime();
LocalDateTime localGmtTime = gmtTime.toLocalDateTime();
For more information about how to use the new API, have a look at this Tutorial about the Java time API.