Parse ISO8601 date string to date with UTC Timezone

iso 8601
convert string to date java
java date to iso 8601
string to date java 8
convert timestamp to iso 8601 java
convert date string to date javascript
how to convert string to date in java in yyyy-mm-dd format
iso 8601 date format java 8

I am trying to serialize/deserialize a date from/to a JavaScript application.

Server side, I use Java, JodaTime is installed on it. I found out how to serialize to ISO with UTC Time zone, but can't find out how to do the reverse operation.

Here is my code

public static String getIsoDate( Date date )
{
    SimpleDateFormat  dateToIsoDateString = new SimpleDateFormat( ISO_8601_DATE_FORMAT );
    TimeZone tz = TimeZone.getTimeZone("UTC");
    dateToIsoDateString.setTimeZone( tz );
    return dateToIsoDateString.format( date );
}

// this will return a date with GMT timezone
public static Date getDateFromIsoDateString( String iso8601date )
{
    DateTimeFormatter jodaParser = ISODateTimeFormat.dateTimeNoMillis();
    return jodaParser.parseDateTime( iso8601date ).toDate();
}

I don't mind using or not Joda, just need a quick and working solution,

If you are using Java 7 or earlier you can refer to this post.

If you are using Java 8 you could do:

    DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_DATE_TIME;
    TemporalAccessor accessor = timeFormatter.parse("2015-10-27T16:22:27.605-07:00");

    Date date = Date.from(Instant.from(accessor));
    System.out.println(date);
Update

As pointed out by @BasilBourque in the comment, TemporalAccessor is java framework level interface, and is not advisable to use in the application code and it is advisable to use concrete classes rather than the interfaces.

This interface is a framework-level interface that should not be widely used in application code. Instead, applications should create and pass around instances of concrete types, such as LocalDate. There are many reasons for this, part of which is that implementations of this interface may be in calendar systems other than ISO. See ChronoLocalDate for a fuller discussion of the issues.

There a few concrete classes available to use, like LocalDate, LocalDateTime, OffsetDateTime, ZonedDateTime and etc..

DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_DATE_TIME;

OffsetDateTime offsetDateTime = OffsetDateTime.parse("2015-10-27T16:22:27.605-07:00", timeFormatter);

Date date = Date.from(Instant.from(offsetDateTime));
System.out.println(date);

Converting ISO 8601-compliant String to java.util.Date, Unfortunately, the time zone formats available to SimpleDateFormat (Java 6 and earlier) are not ISO 8601 compliant. SimpleDateFormat understands time zone  If dotPos = 0 Then dotPos = Len(timePart) + 1 timePart = Left(timePart, dotPos - 1) ' Have them parsed separately by Excel Dim d As Date: d = DateValue(datePart) Dim t As Date: If timePart <> "" Then t = TimeValue(timePart) Dim dt As Date: dt = d + t ' Add the timezone Dim tz As String: tz = Mid(iso, zPos) If tz <> "" And Left(tz, 1) <> "Z" Then Dim colonPos As Integer: colonPos = InStr(tz, ":") Dim minutes As Integer If colonPos = 0 Then If (Len(tz) = 3) Then minutes = CInt(Mid(tz, 2)) * 60

Date.parse(), The Date.parse() method parses a string representation of a date, and returns the representing a simplification of the ISO 8601 calendar date extended format. When the time zone offset is absent, date-only forms are interpreted as a UTC  Given a non-standard date string of "March 7, 2014", parse() assumes a local time zone, but given a simplification of the ISO 8601 calendar date extended format such as "2014-03-07", it will assume a time zone of UTC (ES5 and ECMAScript 2015).

Native solution in Java8

Date.from(ZonedDateTime.parse("1994-11-05T08:15:30+05:30").toInstant())

Date.from(ZonedDateTime.parse("1994-11-05T13:15:30Z").toInstant())

How to Convert a String to Date in Java, LocalTime represents time, without a time-zone in ISO-8601 format. It doesn't store time based on the offset since epoch, and it offers nanosecond  Parsing ISO 8601 date in Javascript. // ISO 8601 with Timezone offset Edit: Which browsers support parsing of ISO-8601 Date String with Date.parse. 12.

moment.utc(string) parses ISO8601 as local time when timezone is , I'm trying to convert this date: 12-04-2012 (DD-MM-YYYY, in UTC) to its Unix timestamp. I'm doing this: var date = '12-04-2012'; var mm = moment().utc( date, "​  java.util.Date. Here’s an example to demonstrate how to convert a java.util.Date to ISO 8601 date string. This is a little bit tricky because we’re using the current time, which is the easiest use-case.

Convert String to Date in Java, By default, Java dates are in the ISO-8601 format, so if we have any string which parse method directly to get a time zone specific date time: ? A string with a time but no date component. The method assumes the current date unless you call the Parse(String, IFormatProvider, DateTimeStyles) overload and include DateTimeStyles.NoCurrentDateDefault in the styles argument, in which case the method assumes a date of January 1, 0001.

pyiso8601: ISO 8601 Parsing for Python, This module parses the most common forms of ISO 8601 date strings (e.g. 2007-​01-14T20:34:22+00:00) into Nothing uses the default timezone given (UTC). A format specification, as described below. If set to "", date times are parsed as ISO8601, dates and times used the date and time formats specified in the locale(). Unlike strptime(), the format specification must match the complete string. na: Character vector of strings to interpret as missing values.

Comments
  • possible duplicate of Converting ISO 8601-compliant String to java.util.Date
  • Your question is not clear. You should provide samples of inputs and desired outputs.
  • The java.time classes discourage using the interfaces/superclasses. The concrete class OffsetDateTime class is appropriate here. OffsetDateTime.parse( "2015-10-27T16:22:27.605-07:00" )
  • @BasilBourque You are right. The documentation suggests not to use the framework-level interfaces in the application code. Updated the answer.
  • ZonedDateTime is not the appropriate class to be using here. For the first, use OffsetDateTime.parse. For the second, Instant.parse. As I had already posted in my Answer.
  • But, According to docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html "A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris." ZonedDateTime is also a valid solution I hope, is it not ? I get the same output for both methods.
  • Notice the name of the official time zone in that quote, Europe/Paris. Our input string under discussion lacks that time zone and has only an offset-from-UTC. An offset is merely a number of hours, minutes, seconds. A time zone is much more, a history of past, present, and future changes to the offset used by the people of a particular region. Your code worked because java.time is implemented with ZoneOffset as a subclass of ZoneId. The code works but is misleading, suggesting a time zone is in play when actually we have no zone at all. Read about OffsetDateTime class.