Skip to content

Instantly share code, notes, and snippets.

@MizukiSonoko
Created October 27, 2015 07:08
Show Gist options
  • Save MizukiSonoko/83759864c7d43132596d to your computer and use it in GitHub Desktop.
Save MizukiSonoko/83759864c7d43132596d to your computer and use it in GitHub Desktop.
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss ZZZ").parseDateTime(s)
String s = "2011-11-29 10:40:24 Etc/GMT";
// split the input in a date and a timezone part
int lastSpaceIndex = s.lastIndexOf(' ');
String dateString = s.substring(0, lastSpaceIndex);
String timeZoneString = s.substring(lastSpaceIndex + 1);
// convert the timezone to an actual TimeZone object
// and feed that to the formatter
TimeZone zone = TimeZone.getTimeZone(timeZoneString);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(zone);
// parse the timezoneless part
Date date = formatter.parse(dateString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment