The column format is dd MMMM yyyy and it should parse dates like 28
September 2018 and should throw error on values such as 28 Sep 2018
DateUtils
uses the date-time API of java.util
and their formatting API, SimpleDateFormat
which are outdated and error-prone. I suggest you should stop using them completely and switch to the modern date-time API.
Using the modern date-time API:
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Test strings
String[] arr = { "28 September 2018", "28 Sep 2018", "28 09 2018" };
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMMM uuuu", Locale.ENGLISH);
for (String s : arr) {
try {
LocalDate date = LocalDate.parse(s, formatter);
// ...Process date e.g.
System.out.println(DateTimeFormatter.ofPattern("MMMM dd, uuuu", Locale.ENGLISH).format(date));
} catch (DateTimeException e) {
System.out.println(s + " is not a valid string.");
}
}
}
}
Output:
September 28, 2018
28 Sep 2018 is not a valid string.
28 09 2018 is not a valid string.
Learn more about the modern date-time API at Trail: Date Time.
If you are doing it for your 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.