When parsing time strings using SimpleDateFormat, I'm seeing some output that I find confusing. So, given the following:
public class NewMain
{
public static void main( String[] args )
{
String str1 = "00:00:03";
parseStr(str1);
String str2 = "-00:00:03";
parseStr(str2);
String str3 = "00:59:59";
parseStr(str3);
String str4 = "-00:59:59";
parseStr(str4);
String str5 = "01:00:00";
parseStr(str5);
String str6 = "-01:00:00";
parseStr(str6);
}
private static void parseStr(String aStr)
{
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Calendar cal = sdf.getCalendar();
Date date = cal.getTime();
System.out.println("SimpleDateFormat.getCalendar.getTime: " + date.toString());
try
{
Date dee = sdf.parse(aStr);
String dStr = dee.toString();
System.out.println("SimpleDateFormat.parse("+aStr+") = " + dStr);
}
catch (ParseException ex)
{
ex.printStackTrace();
}
System.out.println("");
}
}
And Output:
SimpleDateFormat.getCalendar.getTime: Sun Dec 08 10:00:28 CST 1940
SimpleDateFormat.parse(00:00:03) = Thu Jan 01 00:00:03 CST 1970
SimpleDateFormat.getCalendar.getTime: Sun Dec 08 10:00:28 CST 1940
SimpleDateFormat.parse(-00:00:03) = Thu Jan 01 00:00:03 CST 1970
SimpleDateFormat.getCalendar.getTime: Sun Dec 08 10:00:28 CST 1940
SimpleDateFormat.parse(00:59:59) = Thu Jan 01 00:59:59 CST 1970
SimpleDateFormat.getCalendar.getTime: Sun Dec 08 10:00:28 CST 1940
SimpleDateFormat.parse(-00:59:59) = Thu Jan 01 00:59:59 CST 1970
SimpleDateFormat.getCalendar.getTime: Sun Dec 08 10:00:28 CST 1940
SimpleDateFormat.parse(01:00:00) = Thu Jan 01 01:00:00 CST 1970
SimpleDateFormat.getCalendar.getTime: Sun Dec 08 10:00:28 CST 1940
SimpleDateFormat.parse(-01:00:00) = Wed Dec 31 23:00:00 CST 1969
The first 2 negative times parse out to the given time without the "-". The string "-01:00:00", however, parses out to a completely different time. It looks like the parser is subtracting it from Jan 1st 1970. I'm so confused by this, because "-00:00:03" and "-00:59:59" did NOT have that effect!
Any help would be appreciated!