其他人遗漏的一件主要事情是处理时区(TZ)。每当您使用 SimpleDateFormat 来往/从日期的字符串表示时,您确实需要了解您正在处理的 TZ。除非您在 SimpleDateFormat 上明确设置 TZ,否则它将在格式化/解析时使用默认TZ。除非您只处理默认时区中的日期字符串,否则您会遇到问题。
您输入的日期代表 GMT 日期。假设您还希望将输出格式化为 GMT,您需要确保在 SimpleDateFormat 上设置 TZ:
public static void main(String[] args) throws Exception
{
String inputDate = "Tue Mar 19 00:41:00 GMT 2013";
// Initialize with format of input
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
// Configure the TZ on the date formatter. Not sure why it doesn't get set
// automatically when parsing the date since the input includes the TZ name,
// but it doesn't. One of many reasons to use Joda instead
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse(inputDate);
// re-initialize the pattern with format of desired output. Alternatively,
// you could use a new SimpleDateFormat instance as long as you set the TZ
// correctly
sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
}