I am using java.text.SimpleDateFormat to parse string representations of date / time values โโinside an XML document. I see all the times that have a value of 12 hours, shifted by 12 hours into the future, i.e. e. 20 minutes after noon, 20 minutes after midnight the next day are analyzed.
I wrote a unit test, which seems to confirm that the error occurs during parsing (I checked the return values โโfrom getTime() with the linux shell date command). Now I am wondering:
- Is there an error in the
parse() method? - Is there something wrong with the input line?
- Am I using the wrong format string for input?
The input is taken from Yahoo YWeather. Here is the test and its output:
public class YWeatherReaderTest { public static final String[] rgDateSamples = { "Thu, 08 Apr 2010 12:20 PM CEST", "Thu, 08 Apr 2010 12:20 AM CEST" }; public void dateParsing() throws ParseException { DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy K:maz", Locale.US); for (String dtsSrc : YWeatherReaderTest.rgDateSamples) { Date dt = formatter.parse(dtsSrc); String dtsDst = formatter.format(dt); System.out.println(dtsSrc); System.out.println(dtsDst); System.out.println(); } } }
Thu, 08 Apr 2010 12:20 PM CEST
Fri, 09 Apr 2010 0:20 AM CEST
Thu, 08 Apr 2010 12:20 AM CEST
Thu, 08 Apr 2010 0:20 PM CEST
The second output line of the second iteration is a bit strange because 00:20 is not PM. However, the millisecond value of the date object corresponds to a (erroneous) time of 20 minutes at noon.
source share