Rectangle 27 4

No, using SimpleDateFormat, parsing this date is not possible (at least not in jdk 6 or lower). We had to write our own adapter for this ourselves.

Note, since this format is a valid part of XML Schema, you can use the DatatypeConverter.parseDateTime() method to parse this date.

+1 Better than using SimpleDateFormat

@KimballRobinson - probably because that does not fit the xsd dateTime format (you are missing the ':' in the timezone offset).

@jtahlborn, I'm getting time stamps from the Salesforce REST api, so I don't control the format. I am using Joda Time's library to parse the timestamps now.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Convert iso8601 string date time format to date in Java - Stack Overfl...

java date format
Rectangle 27 1

Quote: The easier solution is possibly to use the data type converter in JAXB, since JAXB must be able to parse ISO8601 date string according to the XML Schema specification. javax.xml.bind.DatatypeConverter.parseDateTime("2010-01-01T12:00:00Z") will give you a Calendar object and you can simply use getTime() on it, if you need a Date object.

Convert iso8601 string date time format to date in Java - Stack Overfl...

java date format
Rectangle 27 389

Unfortunately, the time zone formats available to SimpleDateFormat (Java 6 and earlier) are not ISO 8601 compliant. SimpleDateFormat understands time zone strings like "GMT+01:00" or "+0100", the latter according to RFC # 822.

Even if Java 7 added support for time zone descriptors according to ISO 8601, SimpleDateFormat is still not able to properly parse a complete date string, as it has no support for optional parts.

Reformatting your input string using regexp is certainly one possibility, but the replacement rules are not as simple as in your question:

  • Some time zones are not full hours off UTC, so the string does not necessarily end with ":00".
  • ISO8601 allows only the number of hours to be included in the time zone, so "+01" is equivalent to "+01:00"
  • ISO8601 allows the usage of "Z" to indicate UTC instead of "+00:00".

The easier solution is possibly to use the data type converter in JAXB, since JAXB must be able to parse ISO8601 date string according to the XML Schema specification. javax.xml.bind.DatatypeConverter.parseDateTime("2010-01-01T12:00:00Z") will give you a Calendar object and you can simply use getTime() on it, if you need a Date object.

You could probably use Joda-Time as well, but I don't know why you should bother with that.

The JAXB-solution is a really creative approach! It works as well, I have tested it with my sample. However, for whoever faces the problem and is allowed to use JodaTime, I would advise to use it, since it feels more natural. But your solution requires not additional libraries (at least with Java 6).

Here's the reverse: Calendar c = GregorianCalendar.getInstance();c.setTime(aDate);return javax.xml.bind.DatatypeConverter.printDateTime(c);

Actually it's not so simple b/c you have to initialize the jaxb datatypeConverter. I ended up using DatatypeFactory myself as DataTypeConverterImpl did internally. What a headache.

@jarnbjo you are the first and only person I have encountered who prefers the standard, pre 1.8, java date classes, over joda-time. I find joda-time a literal joy to use, especially when compared to the standard api which is an abomination.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Converting ISO 8601-compliant String to java.util.Date - Stack Overflo...

java date iso8601
Rectangle 27 387

Unfortunately, the time zone formats available to SimpleDateFormat (Java 6 and earlier) are not ISO 8601 compliant. SimpleDateFormat understands time zone strings like "GMT+01:00" or "+0100", the latter according to RFC # 822.

Even if Java 7 added support for time zone descriptors according to ISO 8601, SimpleDateFormat is still not able to properly parse a complete date string, as it has no support for optional parts.

Reformatting your input string using regexp is certainly one possibility, but the replacement rules are not as simple as in your question:

  • Some time zones are not full hours off UTC, so the string does not necessarily end with ":00".
  • ISO8601 allows only the number of hours to be included in the time zone, so "+01" is equivalent to "+01:00"
  • ISO8601 allows the usage of "Z" to indicate UTC instead of "+00:00".

The easier solution is possibly to use the data type converter in JAXB, since JAXB must be able to parse ISO8601 date string according to the XML Schema specification. javax.xml.bind.DatatypeConverter.parseDateTime("2010-01-01T12:00:00Z") will give you a Calendar object and you can simply use getTime() on it, if you need a Date object.

You could probably use Joda-Time as well, but I don't know why you should bother with that.

The JAXB-solution is a really creative approach! It works as well, I have tested it with my sample. However, for whoever faces the problem and is allowed to use JodaTime, I would advise to use it, since it feels more natural. But your solution requires not additional libraries (at least with Java 6).

Here's the reverse: Calendar c = GregorianCalendar.getInstance();c.setTime(aDate);return javax.xml.bind.DatatypeConverter.printDateTime(c);

Actually it's not so simple b/c you have to initialize the jaxb datatypeConverter. I ended up using DatatypeFactory myself as DataTypeConverterImpl did internally. What a headache.

@jarnbjo you are the first and only person I have encountered who prefers the standard, pre 1.8, java date classes, over joda-time. I find joda-time a literal joy to use, especially when compared to the standard api which is an abomination.

Converting ISO 8601-compliant String to java.util.Date - Stack Overflo...

java date iso8601
Rectangle 27 3

Apache Jackrabbit uses the ISO 8601 format for persisting dates, and there is a helper class to parse them:

While the subset of Jackrabbit might work, it makes more sense to use a full-fledged purpose-built library. In Java that means either Joda-Time or java.time.

Converting ISO 8601-compliant String to java.util.Date - Stack Overflo...

java date iso8601
Rectangle 27 3

Apache Jackrabbit uses the ISO 8601 format for persisting dates, and there is a helper class to parse them:

While the subset of Jackrabbit might work, it makes more sense to use a full-fledged purpose-built library. In Java that means either Joda-Time or java.time.

Converting ISO 8601-compliant String to java.util.Date - Stack Overflo...

java date iso8601
Rectangle 27 0

Quote: The easier solution is possibly to use the data type converter in JAXB, since JAXB must be able to parse ISO8601 date string according to the XML Schema specification. javax.xml.bind.DatatypeConverter.parseDateTime("2010-01-01T12:00:00Z") will give you a Calendar object and you can simply use getTime() on it, if you need a Date object.

Convert iso8601 string date time format to date in Java - Stack Overfl...

java date format
Rectangle 27 0

Enter the original date into a Date object and then print out the result with a DateFormat. You may have to split up the string into smaller pieces to create the initial Date object, if the automatic parse method does not accept your format.

Date inputDate = convertYourInputIntoADateInWhateverWayYouPrefer(inputString);
DateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss.SSS");
String outputString = outputFormat.format(inputDate);

String str_date="2011-03-10T11:54:30.207Z"; DateFormat formatter ; Date date ; formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS"); date = (Date)formatter.parse(str_date); System.out.println("output: " +date ); Exception :java.text.ParseException: Unparseable date: "2011-03-10T11:54:30.207Z"

As far i know SimpleDateFormat is NOT thread safe, if you need a thread safe solution go with joda-time and build a formatter for example with the PeriodFormatterBuilder and enjoy easy date handling as it should be.

Well, the automatic parser is lenient, but not yet sentient. I guess you'll have to write that convertYourInputIntoADateInWhateverWayYouPrefer() method after all.

+1 @Moritz, I was trying to go for a simple-to-understand solution (as I usually do) but there are definitely advantages to joda time if this is for production.

java - How can I convert a timestamp from yyyy-MM-ddThh:mm:ss:SSSZ for...

java datetime
Rectangle 27 0

No, using SimpleDateFormat, parsing this date is not possible (at least not in jdk 6 or lower). We had to write our own adapter for this ourselves.

Note, since this format is a valid part of XML Schema, you can use the DatatypeConverter.parseDateTime() method to parse this date.

+1 Better than using SimpleDateFormat

@KimballRobinson - probably because that does not fit the xsd dateTime format (you are missing the ':' in the timezone offset).

@jtahlborn, I'm getting time stamps from the Salesforce REST api, so I don't control the format. I am using Joda Time's library to parse the timestamps now.

Convert iso8601 string date time format to date in Java - Stack Overfl...

java date format
Rectangle 27 0

Quote: The easier solution is possibly to use the data type converter in JAXB, since JAXB must be able to parse ISO8601 date string according to the XML Schema specification. javax.xml.bind.DatatypeConverter.parseDateTime("2010-01-01T12:00:00Z") will give you a Calendar object and you can simply use getTime() on it, if you need a Date object.

Convert iso8601 string date time format to date in Java - Stack Overfl...

java date format
Rectangle 27 0

Quote: The easier solution is possibly to use the data type converter in JAXB, since JAXB must be able to parse ISO8601 date string according to the XML Schema specification. javax.xml.bind.DatatypeConverter.parseDateTime("2010-01-01T12:00:00Z") will give you a Calendar object and you can simply use getTime() on it, if you need a Date object.

Convert iso8601 string date time format to date in Java - Stack Overfl...

java date format
Rectangle 27 0

Even easier, with the Joda-Time 2.5 library:

long millisSinceUnixEpoch = new DateTime( "2014-10-23T00:35:14.800Z" ).getMillis();

Joda-Time parses and generates ISO 8601 strings by default. Joda-Time works in Android. The java.util.Date/.Calendar classes are notoriously troublesome, confusing, and flawed. Avoid them.

java - How to convert ISO8601 format into milliseconds? - Stack Overfl...

java android datetime
Rectangle 27 0

Firstly, you need to be aware that UTC isn't a format, it's a time zone, effectively. So "converting from ISO8601 to UTC" doesn't really make sense as a concept.

However, here's a sample program using Joda Time which parses the text into a DateTime and then formats it. I've guessed at a format you may want to use - you haven't really provided enough information about what you're trying to do to say more than that. You may also want to consider time zones... do you want to display the local time at the specified instant? If so, you'll need to work out the user's time zone and convert appropriately.

import org.joda.time.*;
import org.joda.time.format.*;

public class Test {
    public static void main(String[] args) {
        String text = "2011-03-10T11:54:30.207Z";
        DateTimeFormatter parser = ISODateTimeFormat.dateTime();
        DateTime dt = parser.parseDateTime(text);

        DateTimeFormatter formatter = DateTimeFormat.mediumDateTime();
        System.out.println(formatter.print(dt));
    }
}

java - How can I convert a timestamp from yyyy-MM-ddThh:mm:ss:SSSZ for...

java datetime
Rectangle 27 0

Joda Time makes this really easy, as it has built-in support for the ISO format:

DateTimeFormatter iso = ISODateTimeFormat.basicDateTimeNoMillis();
DateTime dateTime = iso.parseDateTime(text);

java - Converting an ISO8601 string into DateTime format? - Stack Over...

java date jodatime
Rectangle 27 0

No, using SimpleDateFormat, parsing this date is not possible (at least not in jdk 6 or lower). We had to write our own adapter for this ourselves.

Note, since this format is a valid part of XML Schema, you can use the DatatypeConverter.parseDateTime() method to parse this date.

+1 Better than using SimpleDateFormat

@KimballRobinson - probably because that does not fit the xsd dateTime format (you are missing the ':' in the timezone offset).

@jtahlborn, I'm getting time stamps from the Salesforce REST api, so I don't control the format. I am using Joda Time's library to parse the timestamps now.

Convert iso8601 string date time format to date in Java - Stack Overfl...

java date format
Rectangle 27 0

No, using SimpleDateFormat, parsing this date is not possible (at least not in jdk 6 or lower). We had to write our own adapter for this ourselves.

Note, since this format is a valid part of XML Schema, you can use the DatatypeConverter.parseDateTime() method to parse this date.

+1 Better than using SimpleDateFormat

Convert iso8601 string date time format to date in Java - Stack Overfl...

java date format
Rectangle 27 0

private void setTimestamp(String timeCreated) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    try {
        Date timeCreatedDate = dateFormat.parse(timeCreated);
        timeStamp = (String) DateUtils.getRelativeTimeSpanString(timeCreatedDate.getTime(),
                                  System.currentTimeMillis(), 
                                  DateUtils.SECONDS_IN_MILLIS);
    } catch ( ParseException e) {}
}

java - How to convert ISO8601 format into milliseconds? - Stack Overfl...

java android datetime
Rectangle 27 0

Yes. you can use SimpleDateFormat like this.

SimpleDateFormat formatter, FORMATTER;
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String oldDate = "2011-03-10T11:54:30.207Z";
Date date = formatter.parse(oldDate.substring(0, 24));
FORMATTER = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
System.out.println("OldDate-->"+oldDate);
System.out.println("NewDate-->"+FORMATTER.format(date));

java - How can I convert a timestamp from yyyy-MM-ddThh:mm:ss:SSSZ for...

java datetime
Rectangle 27 0

You might want to have a look at joda time, which is a little easier to use than the java native date tools, and provides many common date patterns pre-built.

To do this using Joda time, you need two DateTimeFormatters - one for your input format to parse your input and one for your output format to print your output. Your input format is an ISO standard format, so Joda time's ISODateTimeFormat class has a static method with a parser for it already: dateHourMinuteSecondMillis. Your output format isn't one they have a pre-built formatter for, so you'll have to make one yourself using DateTimeFormat. I think DateTimeFormat.forPattern("mm/dd/yyyy kk:mm:ss.SSS"); should do the trick. Once you have your two formatters, call the parseDateTime() method on the input format and the print method on the output format to get your result, as a string.

DateTimeFormatter input = ISODateTimeFormat.dateHourMinuteSecondMillis();
DateTimeFormatter output = DateTimeFormat.forPattern("mm/dd/yyyy kk:mm:ss.SSS");
String outputFormat = output.print( input.parseDate(inputFormat) );

I have downloaded joda time but don't know how to convert ISO8601 to UTC format. Please guide

@user617966: UTC isn't a format, it's a time zone (effectively).

java - How can I convert a timestamp from yyyy-MM-ddThh:mm:ss:SSSZ for...

java datetime