你應該分析它從一個正確的格式,然後用另一個正確的格式轉換爲TO字符串。你在做什麼 - 試圖用yyyy-MM-dd
格式來解釋MM/dd/yyyy
格式的日期,即不正確。
樣品
public static void main(String[] args) throws ParseException {
SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat destinationFormat = new SimpleDateFormat("yyyy-MM-dd");
String sourceDateString = "02/16/2012";
String destinationDateString;
Date dat; // this object stores "perfect" date object
// interpreting string with input format and converting it to date
dat = sourceFormat.parse(sourceDateString);
// expressing date object as string in destination format
destinationDateString = destinationFormat.format(dat);
System.out.format("Done from %s to %s\n", sourceDateString, destinationDateString));
}