I have a string and a format. I want to convert the string to date time using the format. But simple date time format is converting the string to date time even the input string is in a wrong format.
val formatter = new SimpleDateFormat("yyyyMMdd")
val dateInString = "2017042er7"
try {
formatter.setLenient(false)
val date = formatter.parse(dateInString)
System.out.println(date)
System.out.println(formatter.format(date));
} catch {
case e:Exception=>println(e)
}
How can I enforce the format?
**UPDATE:: ** using joda time formatter solved the issue. You can also use java time formatter(as one of the answers suggested).