String to date (Invalid format)
I am 开发者_高级运维using Joda-Time library to convert my String dates to a real date, because this seemed like the easiest solution to do this. I am using the DateTime
object to do this;
new DateTime(strValue);
But when inserting some formats it throws me the exception;
java.lang.IllegalArgumentException: Invalid format: "Mon, 30 Sep 2002 01:56:02 GMT"
java.lang.IllegalArgumentException: Invalid format: "Sun, 29 Sep 2002 19:59:01 GMT"
java.lang.IllegalArgumentException: Invalid format: "Mon, 30 Sep 2002 01:52:02 GMT"
java.lang.IllegalArgumentException: Invalid format: "Sun, 29 Sep 2002 17:05:20 GMT"
java.lang.IllegalArgumentException: Invalid format: "Sun, 29 Sep 2002 19:09:28 GMT"
java.lang.IllegalArgumentException: Invalid format: "Sun, 29 Sep 2002 15:01:02 GMT"
java.lang.IllegalArgumentException: Invalid format: "Sun, 29 Sep 2002 23:48:33 GMT"
java.lang.IllegalArgumentException: Invalid format: "Sun, 29 Sep 2002 17:24:20 GMT"
java.lang.IllegalArgumentException: Invalid format: "Sun, 29 Sep 2002 11:13:10 GMT"
Is there a way to solve this, or should I use something else instead of DateTime.
That constructor is not some kind of universal date-time string interpreting engine. It expects variants of a regular ISO date defined as YYYY-MM-DDTHH:MM:SS.SSSZ
.
You will need to define a format string that describes your format that can then be parsed from. It would be something like EEE, dd MMM YYYY HH:mm:ss zzz
javadoc here!
If I recall correctly, the default parser you're using here expects ISO 8601-formatted time, which this isn't. Otherwise you need to make your own DateTimeFormatter.
tl;dr
ZonedDateTime.parse(
"Mon, 30 Sep 2002 01:56:02 GMT" ,
DateTimeFormatter.RFC_1123_DATE_TIME
)
2002-09-30T01:56:02Z
RFC 1123
The format of your strings such as Mon, 30 Sep 2002 01:56:02 GMT
is defined in the outmoded standards RFC 1123 & RFC 822.
By the way, this is a terrible format. When serializing date-time values to text, use the ISO 8601 standard formats instead. You will find newer standards and protocols adopting ISO 8601 formats nowadays.
java.time
The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.
The java.time class DateTimeFormatter
has a built-in formatter for this archaic format: DateTimeFormatter.RFC_1123_DATE_TIME
.
String input = "Mon, 30 Sep 2002 01:56:02 GMT";
DateTimeFormatter f = DateTimeFormatter.RFC_1123_DATE_TIME;
Use that formatter to instantiate a ZonedDateTime
object.
ZonedDateTime zdt = ZonedDateTime.parse( input , f );
Dump to console.
String output = zdt.toString() ; // Generate a string in standard ISO 8601 format.
System.out.println( "zdt.toString(): " + output );
See this code run live at IdeOne.com.
zdt.toString(): 2002-09-30T01:56:02Z
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
精彩评论