I have a strange problem. I have this method, which should generate a Date out of the name of a directory.
private Date getSubfolderDate(File file) {
String name = file.getName();
SimpleDateFormat parser = new SimpleDateFormat("dd-MM-YY");
Date date;
try {
date = parser.parse(name);
} catch (ParseException e) {
return null;
}
return date;
}
it returns for String name = “17-06-12”: “Mon Jan 02 00:00:00 CET
2012”
and for String name = “18-06-12”: “Mon Jan 02 00:00:00 CET 2012”
Why?
Later i am comparing these with the compareTo(Date xy) and it returns 0, so it has to be the same…..
Your format is also incorrect it should be
dd-MM-yy(for 17-06-12), To tracedown the issue check the catch block withprintStackTrace()Also
SimpleDateFormatparses date from String the resultant Date will invoke toString() that have fixed formatSo if you wish to print formatted date again you need to use
simpleDateFormat.format(dateInstance)Dateclass doesn’t have any property that holds the format to be printed intoString()