As per Project requirement, i want to convert date in this format "9th Nov 20". Please can any one suggest solution for this?
Asked
Active
Viewed 195 times
0
-
I’m immodest enugh to recommend [my own answer here](https://stackoverflow.com/a/50369812/5772882). – Ole V.V. Nov 13 '20 at 20:31
-
You tagged your question simpledateformat, but you should consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Nov 13 '20 at 20:34
2 Answers
0
Date date= new Date();
SimpleDateFormat spf = new SimpleDateFormat("dd MMM yyyy");
String result = spf.format(date);
System.out.println(date);

Ishaan Kumar
- 968
- 1
- 11
- 24
0
Give this code a try:
SimpleDateFormat format = new SimpleDateFormat("d");
String date = format.format(new Date());
if(date.endsWith("1") && !date.endsWith("11"))
format = new SimpleDateFormat("EE MMM d'st', yyyy");
else if(date.endsWith("2") && !date.endsWith("12"))
format = new SimpleDateFormat("EE MMM d'nd', yyyy");
else if(date.endsWith("3") && !date.endsWith("13"))
format = new SimpleDateFormat("EE MMM d'rd', yyyy");
else
format = new SimpleDateFormat("EE MMM d'th', yyyy");
String yourDate = format.format(new Date());

Regex
- 528
- 4
- 20
-
Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Nov 13 '20 at 20:29
-
I understand, I checked your answer about this topic, it seems longer than this one. https://stackoverflow.com/a/50369812/13564911 – Regex Nov 13 '20 at 23:34
-
Yes, @IlyasseSalama, exactly. In my answer there I try to say why I prefer the longer version. There’s also a shorter one for you to take if you prefer. Taste differs. And my recommendation to stay away from `SimpleDateFormat` is invariable. – Ole V.V. Nov 14 '20 at 07:51