【问题标题】:Format month of date to string of 3 first letters- Kotlin将日期月份格式化为 3 个首字母的字符串 - Kotlin
【发布时间】:2019-05-19 11:04:33
【问题描述】:

我有这个日期“08/08/2019”,我希望它看起来像这样:“2019 年 8 月 8 日”,我尝试使用 when,但想知道是否有更简单的方法可以做到这一点?我知道这是一个小问题,但我试图通过互联网找到答案,但找不到。

【问题讨论】:

  • 我知道 08/08/2019 是 8 月 8 日,但 09 Aug 是 09/08/2019 还是 08/09/2019?
  • 这取决于您在编写所需格式时定义的内容,如下面的答案所示
  • 我的搜索很快就找到了Java: Convert String Date to Month Name Year (MMM yyyy) [duplicate],在我看来,一些答案至少可以为您提供一些帮助。也许你应该训练你的搜索能力?
  • 也许我应该。无论如何感谢您的帮助!

标签: date kotlin formatdatetime


【解决方案1】:

首先,您需要将字符串转换为 Date 对象,然后使用新的 java.time 将其转换为您的格式

更新

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

旧答案

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

【讨论】:

  • 有没有办法从EditText 元素打开它?我试过onClick,但它只在第二次点击时有效
  • @ofir2471 我没听懂你的问题你想做什么?
  • sorry弄错了,我在做日期选择器,忘了这里没问,抱歉,我删了
  • 看起来不错。我没有进一步的cmets(但赞成)。
【解决方案2】:

使用预定义的本地化格式和 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);

对 Java 语法感到抱歉,我相信你会翻译。输出是:

2019 年 8 月 8 日

这不是您要求的 08, Aug 2019。但是,Java 通常对全球人们期望的格式有一个很好的了解,所以我的第一个建议是你考虑解决这个问题(坦率地说,08 和逗号对我来说也有点奇怪,但我知道什么?)

代码 sn-p 演示的另一个功能是使用现代 Java 日期和时间 API java.time 中的 LocalDateDateTimeFormatter。我强烈推荐 java.time,而不是像 Date 和特别是 SimpleDateFormat 这样的长期过时的日期时间类。他们设计得很糟糕。他们被替换是有原因的。

如果您的用户说他们绝对想要08, Aug 2019,您需要通过格式模式字符串指定:

    DateTimeFormatter handBuiltFormatter = DateTimeFormatter.ofPattern("dd, MMM uuuu", englishIsrael);
    String dateStringWeWant = date.format(handBuiltFormatter);

现在我们确实得到了您要求的输出:

2019 年 8 月 8 日

链接: Oracle tutorial: Date Time 解释如何使用现代 Java 日期和时间 API java.time。

【讨论】:

  • 试过var date = event.date?.format(DateTimeFormatter.ofPattern("dd, MMM uuuu")),但日期保持不变,16.09.2019
  • 当然活动日期保持不变。 LocalDate 是不可变的,也不能有格式。您只能使用String 的格式。见Save a formatted String to a LocalDate
  • 通过使用SimpleDateFormat 的老式方式,您可以控制string 的变化,所以我可能会坚持下去。
  • 对不起,我没听懂,恐怕你误会了。如果我正确理解 Kotlin 语法,var date = event.date?.format(DateTimeFormatter.ofPattern("dd, MMM uuuu")) 也会给你你想要的字符串。我只说“字符串”。
  • 是的,我知道日期是不可变的,我只能更改我在字符串中看到它的方式,但字符串保持不变,没有任何格式。
【解决方案3】:

你可以使用 Java 的 SimpleDataFormat 类:

import java.text.SimpleDateFormat

代码中的某处:

val myDateStr = "08/08/2019"
val parsedDateObj = SimpleDateFromat("dd/MM/yyyy").parse(myDateStr)
val formattedDateStr = SimpleDateFormat("dd, MMM yyyy").format(parsedDateObj) // "08, Aug 2019"

【讨论】:

  • 我收到此错误:java.lang.IllegalArgumentException: Cannot format given Object as a Date
  • @ofir2471 啊抱歉,凭记忆写了答案,我忘了它需要一个对象!编辑了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
相关资源
最近更新 更多