I am trying to create date time format for optional time part currently I implemented this
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.text.ParseException;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(Ideone.getDate("2017-07-01T00:00:00.0Z"));
System.out.println(Ideone.getDate("2017-07-01T00:00:00.00Z"));
System.out.println(Ideone.getDate("2017-07-01T00:00:00.000Z"));
}
public static LocalDateTime getDate(String date) {
try {
DateTimeFormatter formatter2 =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS]'Z'");
LocalDateTime ldt = LocalDateTime.parse(date, formatter2);
return ldt;
} catch (DateTimeParseException ex) {
return null;
}
}
}
And having output
null
2017-07-01T00:00
2017-07-01T00:00
Now my question is, why date with 1 time fraction is not working and it is working with 2 and 3 fractions? should it entertain 1,2 and 3 fractions? or only 3 fractions?
Thanks in advance
empty() used to avoid null keyword, and you can combine Optional usage with Null Object Design Pattern .
The Optional class in Java is a container that can hold, at max, one value and gracefully deals with null values. The class was introduced in the java. util package to remove the need for multiple null checks to protect against the dreaded NullPointerExceptions during run-time.
Let's learn how to use Java 8's Optionals to make your null checks simple and less error-prone! The method orElse() is invoked with the condition "If X is null, populate X. Return X.", so that the default value can be set if the optional value is not present.
This seems like a bug. The millisecond part of datetime parsing seems to be in general buggy in Java 8, see this issue and its duplicates.
Relevant quote:
Worse however is that the SSS pattern has therefore ended up using strict mode when lenient mode would be appropriate. As it currently stands, DateTimeFormatter.ofPattern("hhmmss.SSS") requires three digits for milliseconds, when it was originally intended to require 0 to 9 (the lenient behaviour).
Given that the current implementation requires three digits for SSS, it is thus very surprising that adjacent value parsing does not apply.
But you seem to have found a case where requirement mentioned above does not apply either. In addition, even though the issue above is in state "fixed", your example seems to have issues in both java 8 and java 9:
>java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
>javac Ideone.java
>java Ideone
null
2017-07-01T00:00
2017-07-01T00:00
>"c:\Program Files\Java\jdk1.8.0\bin"\javac Ideone.java
>"c:\Program Files\Java\jdk1.8.0\bin"\java Ideone
null
2017-07-01T00:00
2017-07-01T00:00
should it entertain 1,2 and 3 fractions? or only 3 fractions?
Based on the quote, it should be only 3, though originally intended to be 0-9.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With