How can I get the time zone from a datetime string with this format 2013-08-15T13:00:00-07:00
?
Asked
Active
Viewed 7,589 times
3
-
Which format do you want? The offset? The display name? The three-letter code? – Mike M. Feb 04 '16 at 06:55
5 Answers
1
You could take the time offset from Andreas answer and use below sinppet to get TimeZone object.
TimeZone tmzo = TimeZone.getTimeZone("GMT"+offset);
System.out.println(tmzo.getID());

Vasco
- 782
- 1
- 5
- 22
1
if Java 8, you can parse directly with ISO_ZONED_DATE_TIME format
String input = "2013-08-15T13:00:00-07:00";
ZonedDateTime zDateTime = ZonedDateTime.parse(input, DateTimeFormatter.ISO_ZONED_DATE_TIME);
ZoneId zone = zDateTime.getZone();

Viet
- 3,349
- 1
- 16
- 35
1
In Java 7 you can parse with and without TZ and than calculate the offset, taking your current TZ offset into account.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date withTZ = sdf.parse("2013-08-15T13:00:00-07:00");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date noTZ = sdf2.parse("2013-08-15T13:00:00-07:00");

wgitscht
- 2,676
- 2
- 21
- 25
0
You can use a regular expression. The following assumes XML dateTime syntax:
String input = "2013-08-15T13:00:00-07:00";
Matcher m = Pattern.compile("(?:[+-]\\d{2}:\\d{2}|Z)$").matcher(input);
if (m.find())
System.out.println("Time zone: " + m.group());
else
System.out.println("No time zone found");

Andreas
- 154,647
- 11
- 152
- 247
-2
2013-08-15T13:00:00-07:00
the last -7:00 means GMT-7
That is MST (Mountain Standard Time)
That timezone is using in US and Canada

Vinay Prajapati
- 7,199
- 9
- 45
- 86

Steve YonG
- 80
- 7
-
1What part of that answer describes how to get the time zone programmatically in Java? – Andreas Feb 04 '16 at 06:44