java.lang.IllegalArgumentException:无法将给定的对象格式化为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27375269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:15:15  来源:igfitidea点击:

java.lang.IllegalArgumentException: Cannot format given Object as a Date

java

提问by Pawan

I recive a date in this format

我以这种格式接收日期

2014-12-09 02:18:38

which i need to convert it to

我需要将其转换为

09-12-2014 02:18:38

I tried converting this way

我尝试以这种方式转换

import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TestDate {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-YYYY HH:mm:ss");
        String input = "2014-12-09 02:18:38";
        String strDate = sdf.format(input);
        System.out.println(strDate);
    }
}

But i am getting this exception during Runtime

但是我在运行时遇到了这个异常

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
    at java.text.DateFormat.format(DateFormat.java:301)
    at java.text.Format.format(Format.java:157)
    at TestDate.main(TestDate.java:15)

Could anybody please help me how to resolve this .

任何人都可以帮我解决这个问题。

采纳答案by Predrag Maric

Try this

尝试这个

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdfOut = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    String input = "2014-12-09 02:18:38";
    Date date = sdfIn.parse(input);

    System.out.println(sdfOut.format(date));
}

Also, note that mis for minutes, while Mis for months.

另外,请注意,这m是几分钟,而M几个月。

回答by Liam de Haas

Instead of

代替

String strDate = sdf.format(input);

use

String strDate = sdf.parse(input);

Also see this question

也看到这个问题

回答by Joop Eggen

    SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-YYYY HH:mm:ss");

    // String to Date:
    String input = "2014-12-09 02:18:38";
    Date date = sdf.parse(input);

    // Date to String:
    String strDate = sdf.format(date);
    System.out.println(strDate);

回答by Brian

SimpleDateFormat in can be used to format a Date object.

SimpleDateFormat in 可用于格式化 Date 对象。

First you have to parse your string to a Data object using format on the SimpleDateFormat you declared. After that you can output it using a second SimpleDataFromat to a string you want.

首先,您必须使用您声明的 SimpleDateFormat 上的格式将字符串解析为 Data 对象。之后,您可以使用第二个 SimpleDataFromat 将其输出到您想要的字符串。

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String input = "2014-12-09 02:18:38";
    Date dt = sdf.parse(input);

    SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");        
    String strDate = sdf2.format(dt);
    System.out.println(strDate);