问题
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.
回答1:
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
.
回答2:
I am aware that there is an accepted answer already. I am posting this to inspire you and anyone else to drop the outdated classes DateFormat
and SimpleDateFormat
. These days we have so much better in the modern DateTimeFormatter
and friends, these classes tend to be much more programmer friendly. So use these if you can — which you can.
System.out.println("la date de création de tweet est: " + created_at);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx uuuu",
Locale.FRENCH);
OffsetDateTime dateTime = OffsetDateTime.parse(created_at, dtf);
This prints:
la date de création de tweet est: dim. janv. 23 24:00:00 +0000 2011
2011-01-24T00:00Z
In French the dots are part of the abbreviations for day of week and month, so they should not be explicit in the format pattern string. Also be aware that you don’t have a leading space in there (unless your date-time string has one too). The result is correct since midnight at 24 hours on 23th of January is the same as 0 hours in the 24th.
The other string from the comment, "Tue Feb 08 12:30:27 +0000 2011", is in the same format, only in English. So you need not change the format pattern, only the locale:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx uuuu",
Locale.ENGLISH);
Now the result is:
la date de création de tweet est: Tue Feb 08 12:30:27 +0000 2011
2011-02-08T12:30:27Z
I didn’t understand the part about converting to integer. I noticed you tried Integer.valueOf(s)
in the code in the question, which will only work if you format the date-time into a string of digits first. For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMdd");
String s = dateTime.format(formatter);
int out = Integer.parseInt(s);
System.out.println("new date " + out);
This prints
new date 20110124
But I probably guessed incorrectly at what you are aiming at. I shall be happy to edit if you explain your requirement better. I used parseInt()
en lieu of valueOf()
, the result is the same, I just avoid the automatic conversion from Integer
to int
.
Edit: What if the date of creation of tweet is for example "dim. janv. 23 24:00:10 +0000 2011"? That is, 10 seconds past midnight. Then we get Invalid value for HourOfDay (valid values 0 - 23): 24
.
While Java apparently accepts 24:00:00 as a time, it thinks that times after midnight should written as for example 0:00:10 on the following day. However, we can easily relax that requirement:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx uuuu",
Locale.FRENCH)
.withResolverStyle(ResolverStyle.LENIENT);
Date time formatters come with three resolver styles, strict, smart (the default) and lenient. Using the last one we get
2011-01-24T00:00:10Z
Again, as expected, 24:00:10 on 23th of January equals 0:00:10 on the 24th.
来源:https://stackoverflow.com/questions/44348852/how-to-solve-the-error-when-convert-string-date-to-integer