Convert date time value to expected with SimpleDateFormat

Posted by Khoi Nguyen on Stack Overflow See other posts from Stack Overflow or by Khoi Nguyen
Published on 2012-11-12T10:26:43Z Indexed on 2012/11/12 11:00 UTC
Read the original article Hit count: 327

Filed under:
|
|

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

  1. MM-yyyy for example 05-2012
  2. 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.

© Stack Overflow or respective owner

Related posts about java

Related posts about simpledateformat