java.time
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Also, quoted below is a notice from the home page of Joda-Time:
Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.
Solution using java.time
, the modern Date-Time API:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "2012-07-04_13-42-03";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d_H-m-s", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
System.out.println(ldt);
}
}
Output:
2012-07-04T13:42:03
ONLINE DEMO
How to compare two LocalDateTime
s:
You can use LocalDateTime#isAfter
and LocalDateTime#isBefore
to compare two LocalDateTime
s.
Learn more about the modern Date-Time API from Trail: Date Time.
What went wrong with your code?
You have used D
which specifies Day in year whereas you need d
which specifies Day in month. Since you are comparing all the Date-Times in the same timezone, there is no problem with the timezone in this case.
* 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.