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?

7
  • have a look at this stackoverflow.com/questions/2597083/… Commented Jun 21, 2016 at 6:32
  • @elwis you want only Date ? Commented Jun 21, 2016 at 6:32
  • Try adding the ( ' ) around the T. Commented Jun 21, 2016 at 6:32
  • Check out the following link i hope it solves your problem. stackoverflow.com/questions/6279647/… Commented Jun 21, 2016 at 6:33
  • @A.Wali: The T is already quoted - check the pattern. Commented Jun 21, 2016 at 6:35

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh that did the trick of course, i blame it on lack of coffee and too much .NET lately. thank you
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.

5 Comments

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.
Not agree, he asked about SimpleDateformat or the DateTimeformatter
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.)
what about Java 7 ? In a native way, 'java.time' is only available for Java 8, isn't it ?
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.
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

2 Comments

They're not using Joda Time - they're using the Java 8 java.time API.
@JonSkeet - both works without any formatters in Java 8 in this case. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.