1

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.

MNM
  • 2,673
  • 6
  • 38
  • 73
  • See: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT. If that doesn't help you use change the pattern to `YYYY-MM-DD'THH:mm:sszzz` – shachar Jul 05 '16 at 05:34
  • Overthinking the problem? `"2016-07-05 02:14:35.0".replace( " " , "T" )` – Basil Bourque Sep 10 '16 at 05:11

3 Answers3

5

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?).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • But the value I want does not have a T in it. I thought that there was a way to put the T in automatically. – MNM Jul 05 '16 at 06:16
  • @MNM: But the value you're *parsing* doesn't. You haven't shown us what result you want. If you need to convert to a different format, you need two different formatters. It would help if you'd provide the expected output... – Jon Skeet Jul 05 '16 at 06:28
  • I want the iso format so like this yyyy MM dd T HH mm ss The T is what I want to put in there. I don't want to break apart the original string and then add a T in there and then concat all that together – MNM Jul 05 '16 at 06:33
  • @MNM: And what about the fractional seconds? And what about separators? (Hyphens, dashes, spaces...) Basically you should edit your question to be a *lot* clearer about this. – Jon Skeet Jul 05 '16 at 06:42
  • @MNM: See my edited answer for more details of how to do this, but it's hard to help with only partial information. – Jon Skeet Jul 05 '16 at 06:53
2

You have some errors in your code:

  • The pattern should be 'yyyy-MM-dd HH:mm:ss.SSS'. Be aware of upper-
    and lowercase.
  • Use LocalDateTime to get date and time. LocalDate only holds the date.

The corrected code:

String created = "2016-07-05 02:14:35.000";
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime newDate = LocalDateTime.parse(created,format);
created = newDate.toString();
System.out.println(created);
AJPerez
  • 3,435
  • 10
  • 61
  • 91
Benjamin Schüller
  • 2,104
  • 1
  • 17
  • 29
-4

Use the following format to convert

     String format = "yyyy-mm-dd hh:mm:ss"

You are using the wrong format to convert. Using T is only to separate the date from time. Use the format like this

     String = "yyyy-MM-dd'T'HH:mm:ss" 
Ashwani Tiwari
  • 1,497
  • 18
  • 28
  • "yyyy-MM-dd'T'HH:mm:ss" use the this – Ashwani Tiwari Jul 05 '16 at 05:44
  • 1
    This is *still* incorrect - it doesn't include the fraction of a second, the first pattern you present still uses minutes instead of months and 12-hour hours, and the second snippet you present wouldn't even compile. – Jon Skeet Jul 05 '16 at 06:31