I need to write code to handle dates. We are slowly moving from Java 7 to Java 8, so my library code should work with both. For this reason, it must have compiled Java 7.
However, projects already ported to Java 8 started using JSR-310 types.
Consider the following statements
if (obj instanceof Date){
} else if (obj instanceof org.joda.time.LocalDateTime){
} else if (obj instanceof java.time.LocalDateTime){
}
I do not use Lambdas or other language materials provided by Java 8. In any case, I use the Java 8 compiler. I want to create Java-compatible code (no UnsupportedClassVersionError
), which breaks when the third one, if the branch gets into Java 7 ( NoClassDefFoundError
).
Currently, the above code does not compile at all if I use the Java 8 javac compiler for the target Java 7.
I am running Ant in Java 8
<javac includeantruntime="false" srcdir="src" destdir="build/compiled" target="1.7" source="1.7"
import java.time.LocalDateTime
, .
, Java 8 Java 7 , , , LocalDateTime
Javapath class class class class, Java 7.
, , (if (obj.getClass().getCanonicalName().equals("java.time.LocalDateTime")
, ), ThreeTen -Backport, JSR-310 Java 7, threeten Java 8, Ivy.
? ( ), .