import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatter {
public static void main(String[] args) throws ParseException {
String testString = "14 September 11";
DateFormat df = new SimpleDateFormat("dd MMMM yy");
Date newDate = df.parse(testString);
}
}
告诉我,为什么我有:
Exception in thread "main" java.text.ParseException: Unparseable date: "14 September 11"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.testtask.ruslan.converter.DateFormatter.main(DateFormatter.java:17)
答案 0 :(得分:4)
它适用于英语语言环境,但对于其他语言,它不会。 不要依赖默认区域设置。您始终应明确指定此类转化的区域设置:
Locale.US
传递:
String testString = "14 September 11";
DateFormat df = new SimpleDateFormat("dd MMMM yy", Locale.US);
Date newDate = df.parse(testString);
new Locale("ru", "RU")
失败:
String testString = "14 September 11";
DateFormat df = new SimpleDateFormat("dd MMMM yy", new Locale("ru", "RU"));
Date newDate = df.parse(testString);