2

I want to convert Date in different Format.

For example,

String fromDate = "2011-04-22"; I want to convert this fromDate as "22nd Apr, 2011"

How can I do this?

Thanks in Advance

5 Answers 5

3

What you want is a little tricky because of the "nd" in 22nd. Depending on the day it'll need a different suffix. SimpleDateFormat doesn't support formatting like this. You'll have to write some additional code to get it. Here's an example, however it's limited to working in certain locales like US:

SimpleDateFormat fromFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat toFormat = new SimpleDateFormat("d'__' MMM, yyyy");

String fromDate = "2011-04-22";
Date date = fromFormat.parse(fromDate);
String toDate = toFormat.format(date);

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);
if (day % 10 == 1 && day != 11) {
    toDate = toDate.replaceAll("__", "st");
} else if (day % 10 == 2 && day != 12) {
    toDate = toDate.replaceAll("__", "nd");
} else if (day % 10 == 3 && day != 13) {
    toDate = toDate.replaceAll("__", "rd");
} else {
    toDate = toDate.replaceAll("__", "th");
}

System.out.println(toDate);
Sign up to request clarification or add additional context in comments.

Comments

1

You can parse the given format using a SimpleDateFormat and then write the 2nd form using a different SimpleDateFormat

SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat to = new SimpleDateFormat("dd MMM, yyyy");

Date dat = from.parse("2011-04-22");
System.out.println(to.format(dat));

Not sure how to if there is a way to add the 'nd' to '22nd' though.

Comments

1

Following this code to remove ordinal of date. It is running successfully.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class patrn {

    private static String deleteOrdinal(String dateString) {
        Pattern p1 = Pattern.compile("([0-9]+)(st|nd|rd|th)");
        Matcher m = p1.matcher(dateString);
        while (m.find()) {
            dateString = dateString.replaceAll(Matcher.quoteReplacement(m.group(0)), m.group(1));
        }
        return dateString;
    }

    public static void main(String[] args) {

        String dateString = "August 21st, 2012";
        SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy");
        Date emp1joinDate = null;
        try {
            emp1joinDate = sdf.parse(deleteOrdinal(dateString));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Comments

0
String dateString = "2011-04-22";
SimpleDateFormat format = new SimpleDateFormat("d MMM, yyyy");

try {
    Date parsed = format.parse(dateString);
}
catch(ParseException pe) {
    System.out.println("ERROR: Cannot parse \"" + dateString + "\"");
}

Comments

0

I would like to point out that format should depend on Locale... Sure, you can do it like this:

String fromDate = "2011-04-22";
DateFormat incomming = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outgoing = DateFormat.getDateInstance(DateFormat.Long, Locale.US);
try {
  Date parsed = incomming.parse(fromDate);
  String toDate = outgoing.format(parsed);
}
catch (ParseException pe) {
  pe.printStackTrace();
}

Of course, instead of Locale.US you need to pass end user's Locale...

BTW. Instead of lousy SimpleDateFormat you might want to use Apache Commons Lang's FastDateFormat. Please also find DateUtils if you are performing a lot of Date-related operations.

2 Comments

Outputing in the locale's long format is useful, but not what the OP asked. They gave a very specific output format that they required.
I know he asked for specific format, which is probably invalid from I18n stand point. Not that Long is, Default should be used...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.