今天遇到了一个关于Java时间转化的问题,要求将格林尼治时间Tue Jun 06 03:17:21 GMT 2006 转化成20060702031721的格式,现将代码复制如下:
public class SimpleDataFormatDemo {
public static void main(String args[]) {
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
String inPutStr = "Tue Jun 06 03:17:21 GMT 2006";
Date parseDate = null;
try {
parseDate = format.parse(inPutStr);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatOut = new SimpleDateFormat("yyyyMMddHHmmss");
formatOut.setTimeZone(TimeZone.getTimeZone("GMT"));
String outPutStr = formatOut.format(parseDate);
System.out.println(outPutStr);
}
}
需要注意的问题:
1,SimpleDateFormat的构造函数接受一个Locale参数,必须将这个Locale参数定制为美国(英国)时区才能解析英文时间参数.
2,为了防止Java国际化带来的时区问题,要将TimeZone设置为GMT.