正如Louis Wasserman所指出的,format.parse將輸入日期String轉換爲Date對象。然後使用該Date對象作爲另一個SimpleDateFormat對象的輸入。
事情是這樣的:
public class DateFormatTest {
public static void main(String[] args) {
String inputDate = args[0];
java.util.Date d = null;
java.text.DateFormat inputDateFormat = new java.text.SimpleDateFormat("MM/dd/yyyy");
java.text.DateFormat outputDateFormat = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
try {
d = inputDateFormat.parse(intputDate);
} catch (java.text.ParseException ex) {
System.err.println("something horrible went wrong!");
}
String output = outputDateFormat.format(d);
System.out.println("The input date of: " + inputDate + " was re-formatted to: " + output);
}
}
提供 「1/1/1970」 作爲輸入參數,輸出是:
The input date of: 1/1/1970 was re-formatted to: 19700101000000
太好了!謝謝你的幫助! – kroe761