在我们的项目中,我们试图以特定的标准格式解析和格式化不同的国际日期,但这导致了Unparsable Date Exception,这是我的示例代码:
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class InternationalizationDate {
public static void main(String[] args) throws ParseException {
SimpleDateFormat dateFormatForDate = new SimpleDateFormat("MMM dd,yyyy");// Expected format
@SuppressWarnings("unused")
String stringDate = "00:00:00 08/09/2015";// This date can be in any format as per the systems local regional settings.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
System.out.println("Local Date Format Pattern : " + simpleDateFormat.toLocalizedPattern());
// DateFormatSymbols dateFormatSymbols = simpleDateFormat.getDateFormatSymbols();
// String[] eras = { "BC" };
// dateFormatSymbols.setEras(eras);
// simpleDateFormat.setDateFormatSymbols(dateFormatSymbols);
Date date = simpleDateFormat.parse(stringDate);
String formattedDate = dateFormatForDate.format(date);
System.out.println("Converted Format : " + formattedDate);
}
}
我们哪里出错?
答案 0 :(得分:0)
导致解析异常错误的原因是解析格式与日期格式不匹配,只需更改此行代码:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
致:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
// this match with "00:00:00 08/09/2015"