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

发布于 2024-11-11 05:13:08 字数 718 浏览 2 评论 0原文

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

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);
}

What's wrong with my code below?

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-11-18 05:13:08

没有什么。这在我的电脑上运行得很好。

编辑:这没有帮助。您可能有需要考虑的特定区域设置。如果您的区域设置需要不同的月份名称/日期名称,您将得到例外。

编辑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();
    }

Nothing. This works just fine on my computer.

EDIT: that wasn't helpful. You may have specific Locale settings that need to be considered. If your Locale expects different month names/day names you will get an exception.

EDIT 2: Try this:

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();
    }
苏佲洛 2024-11-18 05:13:08

这应该有效:

try {

   // dataFormatOrigin (Wed Jun 01 14:12:42 2011)  
   // this is original string with the date information



   // (01/06/2011 14:12:42) - the destination format
   SimpleDateFormat sdfDestination = new SimpleDateFormat(
    "dd-MM-yyyy hh:mm:ss");

   sdfDestination.setLenient( true ); 
   // ^ Makes it not care about the format when parsing

   Date date = sdfDestination.parse(dataFormatOrigin);

   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);
}

This should work:

try {

   // dataFormatOrigin (Wed Jun 01 14:12:42 2011)  
   // this is original string with the date information



   // (01/06/2011 14:12:42) - the destination format
   SimpleDateFormat sdfDestination = new SimpleDateFormat(
    "dd-MM-yyyy hh:mm:ss");

   sdfDestination.setLenient( true ); 
   // ^ Makes it not care about the format when parsing

   Date date = sdfDestination.parse(dataFormatOrigin);

   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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文