Convert a string into iso date format

So I am trying to convert a string into an iso format for the date. This is the string that I am trying to convert "2016-07-05 02:14:35.0" I would like to have it in this format the iso 8601 "2016-07-05T02:14:35.0"

I have this so far

 DateTimeFormatter format = DateTimeFormat.forPattern("YYYY-MM-DDTHH:mm:sszzz");
     new LocalDate();
    LocalDate newDate = LocalDate.parse(created,format);
     created = newDate.toString();

But it is giving me this exception

ERROR: Illegal pattern component: T; nested exception is java.lang.IllegalArgumentException: Illegal pattern component: T

I followed the examples and I don't know what I am doing wrong here. Any help would be appreciated.

Jon Skeet
people
quotationmark

Firstly, that value is a LocalDateTime, not a LocalDate. If you want to get a date out in the end, I'd convert it to a LocalDateTime first, then take the date part of that.

When performing date formatting and parsing, always read the documentation really carefully. It looks like you're using Joda Time (due to using forPattern; if you can move to Java 8 that would be beneficial). That means you should be reading the DateTimeFormat docs.

Current problems with your pattern:

  • You're using 'D' instead of 'd'; that means day-of-year
  • You've specified 'T' without quoting it, and it isn't in the pattern anyway
  • You've ignored the fraction-of-second part of your value
  • You've specified 'zz' when there's no time zone indicator in the value.

Here's a working example:

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

public class Test {
    public static void main(String[] args) {
        String text = "2016-07-05 02:14:35.0";
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.S");
        LocalDateTime localDateTime = LocalDateTime.parse(text, format);
        System.out.println(localDateTime);
    }
}

If you actually want to parse values with T in the middle, you'd use a pattern of "yyyy-MM-dd'T'HH:mm:ss.S" - note how then the T is quoted so it's treated literally instead of as a format specifier.

Note that this is just parsing. It's not "converting a string into ISO date format" - it's converting a string into a LocalDateTime. If you then want to format that value in an ISO format, you need to be using DateTimeFormatter.print, with an appropriate format. For example, you might want to convert to a format of yyyy-MM-dd'T'HH:mm:ss.S':

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

public class Test {
    public static void main(String[] args) {
        String text = "2016-07-05 02:14:35.0";
        DateTimeFormatter parser = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.S");
        LocalDateTime localDateTime = LocalDateTime.parse(text, parser);
        DateTimeFormatter printer = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.S");
        String formatted = printer.print(localDateTime);
        System.out.println(formatted); // Output 2016-07-05T02:14:35.0
    }
}

The code above will only handle a single digit fraction-of-second. You could parse using .SSS instead of .S, but you really need to work out what you want the output to be in different cases (e.g. for 100 milliseconds, do you want .1 or .100?).

people

See more on this question at Stackoverflow