I am trying to accept a user input of date in the format like : "2000 hrs, Thursday, July 20, 2015". I will then convert this to a date format to do operations on it. But the convertion from string to date is defaulting month to January and date to 1. Here is the code snippet :
String userDateFormat = "HHmm 'hrs', EEEE, MMMM dd, YYYY";
SimpleDateFormat userDateFormatter = new SimpleDateFormat(userDateFormat);
String reference_date = "2000 hrs, Thursday, July 20, 2015";
Date date = null;
try {
date = userDateFormatter.parse(reference_date);
} catch (ParseException e) {
System.out.println("Date must be in the format " + userDateFormat);
}
System.out.println(userDateFormatter.format(date));
The following method block prints :
2000 hrs, Thursday, January 01, 2015.
Any clue why?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…