Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

java - how to solve the error when convert string date to integer?

I would like to convert my string date to int but I get an error, example of my date 'dim. janv. 23 24:00:00 +0000 2011` This is part of my code :

   String created_at =  (String) jsonObject.get("created_at");
   System.out.println("la date de création de tweet est: " + created_at);
   DateFormat df = new SimpleDateFormat(" ddd. MMMM. EE HH:mm:ss z yyyy");
   String s= df.format(created_at);
   int out=Integer.valueOf(s);
   System.out.println("new date " +out);

And the output is:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, you already have the date in String format and that's what the format method does. I am assuming what you want to do here is to parse the date (into Date object) and not format.

Also, it looks like the date is in French locale, so you need to use appropriate locale along with SimpleDateFormat and use parse method, e.g.:

DateFormat df = new SimpleDateFormat("EEE MMMM dd HH:mm:ss z yyyy", Locale.FRANCE);
Date date = df.parse("dim. janv. 23 24:00:00 +0000 2011");
System.out.println(date);

This would give you the Date object. If you want to format it differently, you can call format method with different format.

Update

Also, it looks like you are calling overloaded version of format method (by passing in a String and not a Date object. This evantually calls format method of TextFormat class (javadoc here) and that's why you get that Exception.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...