I have this date "08/08/2019" and I want it to look like this: "08, Aug 2019", I tried to use when
but wondered if there is an easier way to do this? I know it's a bit small question but I tried to find an answer over the internet and I couldn't find this.

- 87
- 2
- 12
-
I understand that 08/08/2019 is 08 Aug, but would 09 Aug be 09/08/2019 or 08/09/2019? – Ole V.V. May 20 '19 at 15:50
-
It's depend on what define when you write the format you want, as in the answer below – ofir2471 May 20 '19 at 15:52
-
My search pretty quickly found [Java: Convert String Date to Month Name Year (MMM yyyy) [duplicate\]](https://stackoverflow.com/questions/36113530/java-convert-string-date-to-month-name-year-mmm-yyyy), It seems to me that a couple of the answers might at least get you some of the way. Maybe you should train your searching abilities? – Ole V.V. May 20 '19 at 16:12
-
Maybe I should. Thank you for your help anyway! – ofir2471 May 20 '19 at 19:46
3 Answers
first, you need to convert the string to Date object then convert it to your format using the new java.time
Update
val firstDate = "08/08/2019"
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
val date = formatter.parse(firstDate)
val desiredFormat = DateTimeFormatter.ofPattern("dd, MMM yyyy").format(date)
println(desiredFormat) //08, Aug 2019
old answer
val firstDate = "08/08/2019"
val formatter = SimpleDateFormat("dd/MM/yyyy")
val date = formatter.parse(firstDate)
val desiredFormat = SimpleDateFormat("dd, MMM yyyy").format(date)
println(desiredFormat) //08, Aug 2019

- 2,474
- 1
- 20
- 33
-
Is there a way to open it from an ```EditText``` element? I tried ```onClick``` but it works only on the second click – ofir2471 May 19 '19 at 11:31
-
-
sorry mistaken, I'm doing date picker and forgot I didn't ask it here, sorry, I'll delete it – ofir2471 May 19 '19 at 11:36
-
Use the predefined localized formats and java.time
Locale englishIsrael = Locale.forLanguageTag("en-IL");
DateTimeFormatter shortDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(englishIsrael);
DateTimeFormatter mediumDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(englishIsrael);
String dateStringWeHave = "08/08/2019";
LocalDate date = LocalDate.parse(dateStringWeHave, shortDateFormatter);
String dateStringWeWant = date.format(mediumDateFormatter);
System.out.println(dateStringWeWant);
Sorry about the Java syntax, I trust you to translate. Output is:
8 Aug 2019
It’s not exactly the 08, Aug 2019
that you asked for. However, Java usually has a good idea about what format people around the globe expect, so my first suggestion is you consider settling with this one (frankly 08
and comma also looks a bit weird to me, but what do I know?)
The other feature that the code snippet demonstrates is the use of LocalDate
and DateTimeFormatter
from java.time, the modern Java date and time API. I warmly recommend java.time over the long outdated date-time classes like Date
and in particular SimpleDateFormat
. They were poorly designed. There was a reason why they were replaced.
If your users say they absolutely want 08, Aug 2019
, you need to specify that through a format pattern string:
DateTimeFormatter handBuiltFormatter = DateTimeFormatter.ofPattern("dd, MMM uuuu", englishIsrael);
String dateStringWeWant = date.format(handBuiltFormatter);
Now we do get exactly the output you asked for:
08, Aug 2019
Link: Oracle tutorial: Date Time explaining how to use java.time, the modern Java date and time API.

- 81,772
- 15
- 137
- 161
-
Tried ```var date = event.date?.format(DateTimeFormatter.ofPattern("dd, MMM uuuu"))``` but the date stays the same, ```16.09.2019``` – ofir2471 May 21 '19 at 07:54
-
Of course the event date stays the same. A `LocalDate` is immutable and also cannot have a format. You can have a format only in a `String`. See [Save a formatted String to a LocalDate](https://stackoverflow.com/questions/48098566/save-a-formatted-string-to-a-localdate). – Ole V.V. May 21 '19 at 07:56
-
With the old-fashioned way using ```SimpleDateFormat``` you can control it the ```string``` changes, so I'l probably gonna stick with it. – ofir2471 May 21 '19 at 08:01
-
Sorry, I didn’t get that, and I’m afraid you misunderstood. If I understand Kotlin syntax correctly, `var date = event.date?.format(DateTimeFormatter.ofPattern("dd, MMM uuuu"))` will also give you the string you want. I only said “string”. – Ole V.V. May 21 '19 at 08:07
-
Yes I understood that the date is immutable and I can only change the way I see it in strings, but the strings stays the same without any formatting. – ofir2471 May 21 '19 at 08:23
-
I’m sorry, I still don’t understand. Which string (or result of other type) are you getting from `var date = event.date?.format(DateTimeFormatter.ofPattern("dd, MMM uuuu"))`? As I said, in my Java code `dateStringWeWant` becomes `08, Aug 2019`. – Ole V.V. May 21 '19 at 08:26
-
I have this date: 16.09.2019, I want it to look like this: 16, Sep 2019. When I do the code above it stays 16.09.2019 – ofir2471 May 21 '19 at 08:28
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193675/discussion-between-ole-v-v-and-ofir2471). – Ole V.V. May 21 '19 at 08:29
You can use Java's SimpleDataFormat class:
import java.text.SimpleDateFormat
Somewhere in your code:
val myDateStr = "08/08/2019"
val parsedDateObj = SimpleDateFromat("dd/MM/yyyy").parse(myDateStr)
val formattedDateStr = SimpleDateFormat("dd, MMM yyyy").format(parsedDateObj) // "08, Aug 2019"

- 11,707
- 12
- 44
- 82
-
I get this error: ```java.lang.IllegalArgumentException: Cannot format given Object as a Date``` – ofir2471 May 19 '19 at 11:17
-
@ofir2471 Ah sorry, wrote the answer from memory, I forgot that it takes an object! Edited my answer. – Melbourne2991 May 19 '19 at 11:22