I needed to parse the HTTP header Last-Modified to a Java Date. When I tried to parse the value of the header “Mon, 02 Jun 2008 15:30:42 GMT” it threw an exception java.text.ParseException: Unparseable date. After playing around with the code a bit I figured out I needed to add TimeZone and Locale to get the value to parse correctly.
This is my test code, which is in Scala by the way:
import java.text.SimpleDateFormat import java.util.Date import java.util.TimeZone import java.util.Locale val sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US) format.sdf(TimeZone.getTimeZone("GMT")) println(sdf.format(new Date())) println(sdf.parse("Mon, 02 Jun 2008 15:30:42 GMT"))
This got me wondering if I needed to make my code smart enough to handle different timezones and locales. Checking the Hypertext Transfer Protocol — HTTP/1.1 RFC (RFC2616) gave me the answer. From section 3.3.1 Full Date:
“All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception. For the purposes of HTTP, GMT is exactly equal to UTC (Coordinated Universal Time). This is indicated in the first two formats by the inclusion of “GMT” as the three-letter abbreviation for time zone, and MUST be assumed when reading the asctime format.”
Nice, my code is sufficient 🙂