Format month of date to string of 3 first letters- Kotlin

别等时光非礼了梦想. 提交于 2020-05-17 13:02:20

问题


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.


回答1:


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:


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.




回答3:


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"


来源:https://stackoverflow.com/questions/56207152/format-month-of-date-to-string-of-3-first-letters-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!