2

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Thizzer
  • 16,153
  • 28
  • 98
  • 139
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 21 '18 at 22:03

3 Answers3

5

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!

Affe
  • 47,174
  • 11
  • 83
  • 83
1

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.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
0

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?

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154