2

Running this gives me the following error, what am I missing ?

public static void main(String[] args) {

    DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    LocalDateTime localDateTime = LocalDateTime.parse("20200331094118137",_timestampFomatGMT);
    System.out.println(localDateTime);     
}

Gives me the following exception. What am I missing ?

Exception in thread "main" java.time.format.DateTimeParseException: Text '20200331094118137' could not be parsed at index 0
        at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
        at java.time.LocalDateTime.parse(LocalDateTime.java:492)
        at cotown.lib.common.util.JavaTimeUtil.main(JavaTimeUtil.java:90)
deHaar
  • 17,687
  • 10
  • 38
  • 51
Abhishek Singh
  • 10,243
  • 22
  • 74
  • 108
  • 2
    You didn't give it the string `"20200331"` did you? You gave it `"20200331094118137"`, which is in the wrong format. – Sweeper Mar 31 '20 at 11:37
  • 1
    Also, your format `yyyyMMdd` represents a `LocalDate`, so if you want a `LocalDateTime` (at start of day?) , you should parse it to a `LocalDate` first, then convert to a `LocalDateTime`. – Sweeper Mar 31 '20 at 11:40
  • 1
    You also wan to parse a LocalDate without the time part. I would suggest using `LocalDate.parse("....", formatter).atStartOfDay()` – Flown Mar 31 '20 at 11:41
  • Apologies Edited the code – Abhishek Singh Mar 31 '20 at 11:57
  • Large edits should be *additions* to your original question. By overwriting your original text, you have caused most of the answers to seem nonsensical and irrelevant. – VGR Mar 31 '20 at 12:06
  • 1
    After the edit, the question seems like a duplicate of [this](https://stackoverflow.com/questions/22588051/is-java-time-failing-to-parse-fraction-of-second). Can you confirm? – Sweeper Mar 31 '20 at 12:20

2 Answers2

6

Java does not accept a plain Date value as DateTime.

Try using LocalDate,

public static void main(String[] args) {
DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate localDateTime = LocalDate.parse("20200331",_timestampFomatGMT);
System.out.println(localDateTime);
}

or if you really have to use LocalDateTime, then try

LocalDateTime time = LocalDate.parse("20200331", _timestampFomatGMT).atStartOfDay();

EDIT

there was a bug for this already raised https://bugs.openjdk.java.net/browse/JDK-8031085. It is fixed in JDK 9.

Shubham
  • 549
  • 4
  • 11
1

You cannot parse a date-only String into a LocalDateTime without passing a time value in addition.

What you can do is use a date-only class like LocalDate similar to your code:

public static void main(String[] args) {
    DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
    LocalDate localDate = LocalDate.parse("20200331",_timestampFomatGMT);
    System.out.println(localDate);
}

That would simply output

2020-03-31

If you really need to have a LocalDateTime and the String to be parsed cannot be adjusted to include time, then pass an additional time of 0 hours and minutes with an intermediate operation like this (but keep in mind that the output will include the time information as well):

public static void main(String[] args) {
    DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
    LocalDate localDate = LocalDate.parse("20200331",_timestampFomatGMT);
    LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(0, 0));
    System.out.println(localDateTime);
}

Or use LocalDateTime time = LocalDate.parse("20200331", _timestampFomatGMT).atStartOfDay(); as suggested by @Shubham.

Output would be:

2020-03-31T00:00

For outputting the date only, change the last line of the last example to

System.out.println(localDateTime.format(DateTimeFormatter.ISO_DATE));

which will only output the date part of the LocalDateTime in an ISO representation:

2020-03-31

EDIT

Targeting your latest question update, this might help:

public static void main(String[] args) {
    DateTimeFormatter timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    LocalDateTime localDateTime = LocalDateTime.parse("20200331094118137", timestampFomatGMT);
    System.out.println(localDateTime);
}

Output:

2020-03-31T09:41:18.137
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • I am doing the same thing which you have done in edit – Abhishek Singh Mar 31 '20 at 12:03
  • @AbhishekSingh I copy & pasted the code of your first draft of that question... That's why I first wrote less relevant stuff. I had the code from about 40 minutes ago. Is the `Exception` occurring when you execute the updated code? If not, please add the new error message to your question. – deHaar Mar 31 '20 at 12:06
  • @AbhishekSingh Your code is actually working... What's the problem exactly? – deHaar Mar 31 '20 at 12:11
  • It doesnt work for me. I get the following exception. – Abhishek Singh Mar 31 '20 at 12:33
  • @AbhishekSingh I can confirm your / my code works compiled and executed on Java 10, **but doesn't when using Java 8**. That's due to the bug mentioned in the answer by *Shubham* and might be a problem if you have to use Java 8. – deHaar Mar 31 '20 at 12:40