java - 使用 SimpleDateFormat 将自定义日期格式转换为另一种格式时出错

标签 java date date-format simpledateformat

我下面的代码有什么问题?

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/

相关文章:

java - 插入排序的问题

javascript - Meteor 和 ReactJS - 从 MongoDB 检索日期到字符串?

java - 如何在 android 中获取 EditText 提示的本地化日期格式

mysql - 连接两个包含时间函数的 mysql 查询

java - 如何将时间四舍五入到最近的一刻钟和半小时间隔?

php - 如何在 PHP 中将日期时间转换为 ISO 8601

java - 使用 jayspt 和 hibernate 查询数据

java - 在同一个应用程序中使用 TCP 和 UDP 有什么问题吗?

java - 为什么我在 JVM 下运行时在我的 Ada 共享库上得到 "storage error"

date - 将当前日期添加到文件名时出现 mysqldump 错误