java.time
The legacy date-time API (java.util
date-time types and their formatting API, SimpleDateFormat
) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time
, the modern date-time API*.
Also, check the following notice at the Home Page of Joda-Time
Joda-Time is the de facto standard date and time library for Java
prior to Java SE 8. Users are now asked to migrate to java.time
(JSR-310).
Demo using modern date-time API:
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.stream.Stream;
public class Main {
public static void main(String args[]) {
Stream.of(
"2014-09-11T03:27:54",
"2014-09-11T03:27:54kanmsdklnasd",
"2014-09-11T03:27:54234243"
).forEach(s -> {
try {
System.out.println(LocalDateTime.parse(s));
}catch(DateTimeParseException e) {
System.out.printf("The date-time string, \"%s\" is not valid.%n", s);
}
});;
}
}
Output:
2014-09-11T03:27:54
The date-time string, "2014-09-11T03:27:54kanmsdklnasd" is not valid.
The date-time string, "2014-09-11T03:27:54234243" is not valid.
Learn more about the the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.