Convert date time value to expected with SimpleDateFormat
- by Khoi Nguyen
I have an issue with converting a date time value to expected one with SimpleDateFormat (java), my expected format is MM/yyyy, and I want to convert 2 values to only 1 format
MM-yyyy for example 05-2012
yyyy-MM for example 2012-05
ouput is 05/2012.
I implemented something look like following
String expiry = "2012-01";
try {
result = convertDateFormat(expiry, "MM-yyyy", expectedFormat);
} catch (ParseException e) {
try {
result = convertDateFormat(expiry, "yyyy-MM", expectedFormat);
} catch (ParseException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
private String convertDateFormat(String date, String oPattern, String ePattern) throws ParseException {
SimpleDateFormat normalFormat = new SimpleDateFormat(oPattern);
Date d = normalFormat.parse(date);
SimpleDateFormat cardFormat = new SimpleDateFormat(ePattern);
return cardFormat.format(d);
}
Now, the return value is 6808, I don't know why.
Kindly anyone help me on this case.