Reputation: 21
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
Upvotes: 2
Views: 2004
Reputation: 11
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();
}
}
}
Upvotes: 1
Reputation: 72039
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);
Upvotes: 3
Reputation: 18662
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.
Upvotes: 0
Reputation: 7809
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 + "\"");
}
Upvotes: 0
Reputation: 30934
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.
Upvotes: 1