0

Hello I try parse String 20110330174824917 to OffsetDateTime using DateTimeFormatter so

 public static void main(String[] args)  {
       // System.out.println(OffsetDateTime.parse("20110330174824917", DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")));
        System.out.println(LocalDateTime.parse("20110330174824917", DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")));
    }

but I get
Exception in thread "main" java.time.format.DateTimeParseException: Text '20110330174824917' could not be parsed, unparsed text found at index 8 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) at java.time.LocalDateTime.parse(LocalDateTime.java:492)


Hey guys it looks like this problem is related with java 8 https://bugs.openjdk.java.net/browse/JDK-8031085

Thank you all for help

M.B
  • 134
  • 9
  • Well, you are using an `OffsetDateTime`, but both your pattern and your string-to-be-parsed don't have a zone offset. `LocalDateTime` is a better fit in this case. – MC Emperor Feb 23 '21 at 14:26
  • By the way, [I cannot reproduce it](https://ideone.com/mpfo5x). – MC Emperor Feb 23 '21 at 14:28
  • I try with LocalDateTime but problem still appear – M.B Feb 23 '21 at 14:34
  • I edit code in topic maybe it will help to reproduce it – M.B Feb 23 '21 at 14:34
  • I have try to your code and it's work perfectly. What is the version of the jdk you use ? – Albanninou Feb 23 '21 at 14:44
  • [I still cannot reproduce it](https://ideone.com/6bfVxL). – MC Emperor Feb 23 '21 at 14:58
  • Have you saved and recompiled the file? Is the datetime string mentioned in your post *exactly* the string you used when compiling and running the program? With other words: are there absolutely no invisible (non-printable) characters present in your string? – MC Emperor Feb 23 '21 at 15:04

2 Answers2

2

DateTimeFormatter#withZone

Your date-time string does not have timezone information and therefore, in order to parse it into OffsetDateTime, you need to pass the timezone information explicitly. You can use DateTimeFormatter#withZone to parse the date-time string into ZonedDateTime which you can convert to OffsetDateTime using ZonedDateTime#toOffsetDateTime.

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "20110330174824917";
        
        // Change the ZoneId as per your requirement e.g. ZoneId.of("Europe/London")
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS", Locale.ENGLISH)
                                                .withZone(ZoneId.systemDefault());
        
        OffsetDateTime odt = ZonedDateTime.parse(strDateTime, dtf)
                                            .toOffsetDateTime();
        System.out.println(odt);
    }
}

Output:

2011-03-30T17:48:24.917+01:00
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

You can define the used Zone for the Formatter and concatenate the input string with the correct Zone Offset

// note the added Z at the end of the pattern for the offset
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSZ").withZone(ZoneId.of("UTC"));

OffsetDateTime dateTime = OffsetDateTime.parse("20110330174824917" + "+0000", formatter);
SzaPe
  • 131
  • 1
  • 8