I have written a small method to return a date object when given a string. The method is as shown below:
public Date getDateObjectFromString(String dateAsString)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date tempDate = null;
try
{
tempDate = sdf.parse(dateAsString);
}
catch(ParseException pe)
{
//do some error reporting here
}
return tempDate;
}
Everything is working ok, but I’ve run into something that I’d like to clarify. When I pass two different strings to this method it is returning the same date when reading the value in the debugger. The two strings I am passing are:
2011-07-21T19:44:00.000-0400
2011-07-21T19:44:00.000-04:00
As you can see these two strings are nearly identical, and when I look at the variable output for these newly created dates in the debugger, it shows the exact same date/time for either string. So, does the colon in the second string (at 04:00) make any difference if the debugger is showing the same date? Should I worry or can I proceed without any weird bugs popping up later on?
The Android docs for SimpleDateFormat mention that they use RFC 822 timezones. When I went to the JavaDocs for SimpleDateFormat, which is what Android is attempting to mimic with this class, I see this note about RFC 822 timezones:
And here is the note for general time zones:
In the definition for the general time zones, you will notice they use a ‘:’.
This means that your two strings, while different, will be parsed to the same time.