Reputation: 597
What is the Simple Date Format for "2014-01-02T23:03:30-05:00"?
I have googled and got the format only, yyyy-MM-dd'T'HH:mm:ssz. But this format is only working when my date format is without colon in the end "2014-01-02T23:03:30-0500".
Can anyone please advise on this?
Upvotes: 2
Views: 367
Reputation: 340118
FYI, you can pass that string directly to a constructor in Joda-Time 2.3. No need for a formatter. Joda-Time uses that standard ISO 8601 format as its default, even tolerating the lack of a colon in the offset.
Instead of passing DateTimeZone.UTC
as I did in my example code below, you would pass the specific time zone of your interest.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
String string = "2014-01-02T23:03:30-0500";
DateTime dateTime = new DateTime( string, DateTimeZone.UTC );
System.out.println( "dateTime: " + dateTime );
When run:
dateTime: 2014-01-03T04:03:30.000Z
Upvotes: 0
Reputation: 22027
There is the letter X
for ISO 8601 timezone
.
import java.text.*;
import java.util.*;
public class TimeZoneTest {
public static void main(final String[] args) throws ParseException {
final DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
final String string = "2014-01-02T23:03:30-05:00";
final Date date = format.parse(string);
System.out.println(date);
}
}
Upvotes: 4
Reputation: 542
Use an X
instead of the Z
docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Upvotes: 1
Reputation: 328873
If you use Java 7, you can replace the z
by a X
to allow for colon separator: yyyy-MM-dd'T'HH:mm:ssX
.
See also the javadoc.
Before Java 7, you need to either parse it manually by first removing the colon or you can use an external library such as Jodatime or threeten.
Upvotes: 6