0

I have this datestring:

2011-11-01T13:00:00.000

and I don't seem to get that one parsed no matter if I try SimpleDateformat or the DateTimeformatter

My last try is this one:

LocalDateTime datetime = LocalDateTime.parse(
  deliverydate,
  DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.S"));

But that gives me an error on index 21. Do I simply need to substring that datestring since I actually only care about the date and not the time?

elwis
  • 1,395
  • 2
  • 20
  • 34

3 Answers3

4

You've specified one digit of subsecond precision - but you've got three. Use SSS and it's fine:

String text = "2011-11-01T13:00:00.000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime datetime = LocalDateTime.parse(text, formatter);

Also note how much more readable the code is when you separate out "creating the formatter" from "parsing the value".

Unlike SimpleDateFormat, DateTimeFormatter is immutable and thread-safe, so if you need to use this more than once I'd suggest extracting it to a static final field:

private static final DateTimeFormatter DELIVERY_DATE_FORMATTER =
    DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ROOT);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Oh that did the trick of course, i blame it on lack of coffee and too much .NET lately. thank you – elwis Jun 21 '16 at 06:38
2

Java 7/8 is ISO 8601 compliant

The best solution for Java 7 ( see @Jon post for Java 8 )

    DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    String string1 = "2016-06-21T12:08:56.235";
    Date result1 = df1.parse(string1);

You can find more examples in section Examples at SimpleDateFormat javadoc.

biology.info
  • 3,500
  • 2
  • 28
  • 39
  • The OP is using the `java.time` API, and there's no reason to take the retrograde step of going back to `java.util.*` here - it's just a matter of fixing the format pattern. – Jon Skeet Jun 21 '16 at 06:36
  • Not agree, he asked about SimpleDateformat or the DateTimeformatter – biology.info Jun 21 '16 at 06:38
  • True, but given that they *can* use `java.time` and their problematic code can be fixed very easily to keep using it, I don't think it's a good idea to suggest they go back to the truly awful `java.util.Date` API. (Additionally, your answer doesn't explain the change at all - it's *just* three lines of code. Code-only answers are rarely as helpful as they could be.) – Jon Skeet Jun 21 '16 at 06:40
  • what about Java 7 ? In a native way, 'java.time' is only available for Java 8, isn't it ? – biology.info Jun 21 '16 at 07:00
  • Yes, but the OP is clearly able to use it, given that they're provided an example using it. It's one thing to use the crufty API if someone *has* to, but when they don't have to, I don't think it's a good idea to provide an answer bringing them back to it. – Jon Skeet Jun 21 '16 at 07:43
0

Which version of Joda-Time have you used?

I've tried with most recent - joda-time:joda-time:2.9.4 and below code works without any patterns:

import java.time.LocalDateTime;

public class Main {

    static final String DATE_TIME = "2011-11-01T12:34:56.789";

    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.parse(DATE_TIME);
        System.out.println(dateTime.toString());
    }
}

Sample breakpoint:

enter image description here

Edit 1

After Jon's comment I've tested above issue without joda-time (in pure Java 8) and also works perfectly.

Code:

import java.time.LocalDateTime;

public class Main {

    static final String DATE_TIME = "2011-11-01T12:34:56.789";

    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.parse(DATE_TIME);
        System.out.println(dateTime.toString());
    }
}

Sample breakpoint:

enter image description here

Community
  • 1
  • 1
Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42