I've got a String
like this: 2013-04-19
, and I want to change it into : April 19th 2013
. I know there is some classes in Java like SimpleDateFormat
, but I don't know what kind of function I should use. Maybe I need to choose the class Pattern
? I need some help.
Asked
Active
Viewed 150 times
1
-
2You can start reading about it here: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – home Jan 13 '13 at 21:07
-
5A quick look at any example for SimpleDateFormat will shoudl taht you need to `parse` and then `format` the String to a date and back to a String – Peter Lawrey Jan 13 '13 at 21:07
-
1Also, you will have to write a custom way to say `19th` as it's not standard in `SimpleDateFormat`. – Buhake Sindi Jan 13 '13 at 21:08
-
3Also; as always when dealing with dates - consider using (JodaTime)[http://joda-time.sourceforge.net/] – Peter Svensson Jan 13 '13 at 21:14
3 Answers
2
Try the following, which should give you correct "th", "st", "rd" and "nd" for days.
public static String getDayOfMonthSuffix(final int n) {
if (n >= 11 && n <= 13) {
return "th";
}
switch (n % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
public static void main(String[] args) throws ParseException {
Date d = new SimpleDateFormat("yyyy-MM-dd").parse("2013-04-19");
int day = Integer.parseInt(new java.text.SimpleDateFormat("dd").format(d));
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM dd'" + getDayOfMonthSuffix(day) + "' yyyy");
String s = sdf.format(d);
System.out.println(s);
}
Which will print April 19th 2013
(Day termination adapted from this post)

Community
- 1
- 1

Charles Menguy
- 40,830
- 17
- 95
- 117
1
Try this:
String originalDate = "2013-04-19";
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(originalDate);
} catch (Exception e) {
}
String formattedDate = new SimpleDateFormat("MMMM dd yyyy").format(date);
Will not print the st, nd, rd, etc.

carloabelli
- 4,289
- 3
- 43
- 70
1
Just using parse method from SimpleDateFormat class try this:
new SimpleDateFormat("MMM dd YYYY").parse("2013-04-19");

Ardiansyah Putra
- 21
- 1