我下面的代码有什么问题?
try {
// dataFormatOrigin (Wed Jun 01 14:12:42 2011)
// this is original string with the date information
SimpleDateFormat sdfSource = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
Date date = sdfSource.parse(dataFormatOrigin);
// (01/06/2011 14:12:42) - the destination format that I want to have
SimpleDateFormat sdfDestination = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
dataFormatDest = sdfDestination.format(date);
System.out.println("Date is converted to MM-dd-yyyy hh:mm:ss");
System.out.println("Converted date is : " + dataFormatDest);
} catch (ParseException pe) {
System.out.println("Parse Exception : " + pe);
}
最佳答案
没有。这在我的电脑上工作得很好。
编辑:这没有帮助。您可能需要考虑特定的区域设置。如果您的区域设置需要不同的月份名称/日期名称,您将得到一个异常(exception)。
编辑 2:试试这个:
try{
String dataFormatOrigin = "Wed Jun 01 14:12:42 2011";
// this is original string with the date information
SimpleDateFormat sdfSource = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.US);
Date date = sdfSource.parse(dataFormatOrigin);
// (01/06/2011 14:12:42) - the destination format that I want to have
SimpleDateFormat sdfDestination = new SimpleDateFormat( "dd-MM-yyyy hh:mm:ss");
String dataFormatDest = sdfDestination.format(date);
System.out .println("Date is converted to MM-dd-yyyy hh:mm:ss"); System.out .println("Converted date is : " + dataFormatDest);
} catch (ParseException pe) {
System.out.println("Parse Exception : " + pe);
pe.printStackTrace();
}
关于java - 使用 SimpleDateFormat 将自定义日期格式转换为另一种格式时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6221494/