Reputation: 107
I am using org.joda.time.DateTime; package to convert ISO 8601 datetime for Eg "2017-02-07T00:00:00.000+05:30" to a format "yyyy-MM-dd HH:mm:ss.SSS".
Code is :
String dateTimePattern = "yyyy-MM-dd HH:mm:ss.SSS";
DateTimeFormatter dtf = DateTimeFormat.forPattern(inputDateTimePattern);
DateTime jodatime = dtf.parseDateTime("2017-02-07T00:00:00.000+05:30");;
System.out.println("Converted datetime is: ",jodatime.toString(dtf))
But i get error mentioning
java.lang.IllegalArgumentException: Invalid format: is malformed at ".T00:00:00.000+05:30"
How to convert ISO 8601 datetime format in required format in java ?
Upvotes: 3
Views: 1137
Reputation: 338730
Joda-Time is replaced by the java.time classes.
OffsetDateTime.parse( "2017-02-07T00:00:00.000+05:30" )
The Joda-Time project is now in maintenance mode, with its team advising migration to the java.time classes.
In java.time, your input string can be parsed directly as a OffsetDateTime
object. No need to specify a formatting pattern.
OffsetDateTime odt = OffsetDateTime.parse( "2017-02-07T00:00:00.000+05:30" );
A time zone is a history of offsets for a particular region. So always better to use if you are certain of the intended time zone.
ZoneId z = ZoneId.of( "Asia/Kolkata" ); // Or "America/Montreal", etc.
ZonedDateTime zdt = odt.atZoneSameInstant();
In Joda-Time, you can parse a string in standard ISO 8601 format with an offset-from-UTC in either of two ways:
parse
methodThese two routes are not the same! See the class doc from the parse
method:
However, when this method is passed a date-time string with an offset, the offset is directly parsed and stored. As such, DateTime.parse("2010-06-30T01:20+02:00") and new DateTime("2010-06-30T01:20+02:00")) are NOT equal. The object produced via this method has a zone of DateTimeZone.forOffsetHours(2). The object produced via the constructor has a zone of DateTimeZone.getDefault().
Upvotes: 1
Reputation: 672
Use the below method
public static Calendar toCalendar(String iso8601string) {
DateTime dt = new DateTime(iso8601string);
Date date = new Date(dt.getMillis());
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
Upvotes: 0
Reputation: 3520
Looks like you got confused while using DateTimeFormat. Given time string is not in yyyy-MM-dd HH:mm:ss.SSS
format. So you are getting the exception which is expected.
SimpleDateFormat target = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat source = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date date=source.parse("2017-02-07T00:00:00+05:30");
System.out.println(target.format(date)); // prints 2017-02-07 00:00:00.000
This code will format the date into yyyy-MM-dd HH:mm:ss.SSS
.
Upvotes: 0
Reputation: 6914
Well... the solution might be a mix between 2 already existing answers here at Stack Overflow
First - look at this post How to parse and generate DateTime objects in ISO 8601 format which describes how to write such a code using C# (C# and JAVA are very similar)
Second - please use the SimpleDateFormat
as described here How to parse a date?
This combination should do the trick
Upvotes: 0