将日期月份格式化为 3 个首字母的字符串 - Kotlin
Format month of date to string of 3 first letters- Kotlin
我有这个日期“08/08/2019”,我希望它看起来像这样:“2019 年 8 月 8 日”,我尝试使用 when
但想知道是否有更简单的方法来做这个?我知道这是个小问题,但我试图在互联网上找到答案,但找不到。
您可以使用 Java 的 SimpleDateFormat class:
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"
首先,您需要将字符串转换为 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
使用预定义的本地化格式和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 语法,我相信你会翻译。输出为:
8 Aug 2019
这不完全是您要求的 08, Aug 2019
。然而,Java 通常对全球人们期望的格式有一个很好的了解,所以我的第一个建议是你考虑使用这个格式(坦率地说,08
和逗号对我来说也看起来有点奇怪,但是我知道什么?)
代码片段演示的另一个功能是使用 java.time 中的 LocalDate
和 DateTimeFormatter
,现代 Java 日期和时间 API .我热烈推荐 java.time 而不是早已过时的 date-time 类,例如 Date
,尤其是 SimpleDateFormat
。他们设计得很糟糕。他们被替换是有原因的。
如果您的用户说他们绝对想要 08, Aug 2019
,您需要通过格式模式字符串来指定:
DateTimeFormatter handBuiltFormatter = DateTimeFormatter.ofPattern("dd, MMM uuuu", englishIsrael);
String dateStringWeWant = date.format(handBuiltFormatter);
现在我们确实得到了您要求的输出:
08, Aug 2019
Link: Oracle tutorial: Date Time解释如何使用java.time,现代Java日期和时间API.
我有这个日期“08/08/2019”,我希望它看起来像这样:“2019 年 8 月 8 日”,我尝试使用 when
但想知道是否有更简单的方法来做这个?我知道这是个小问题,但我试图在互联网上找到答案,但找不到。
您可以使用 Java 的 SimpleDateFormat class:
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"
首先,您需要将字符串转换为 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
使用预定义的本地化格式和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 语法,我相信你会翻译。输出为:
8 Aug 2019
这不完全是您要求的 08, Aug 2019
。然而,Java 通常对全球人们期望的格式有一个很好的了解,所以我的第一个建议是你考虑使用这个格式(坦率地说,08
和逗号对我来说也看起来有点奇怪,但是我知道什么?)
代码片段演示的另一个功能是使用 java.time 中的 LocalDate
和 DateTimeFormatter
,现代 Java 日期和时间 API .我热烈推荐 java.time 而不是早已过时的 date-time 类,例如 Date
,尤其是 SimpleDateFormat
。他们设计得很糟糕。他们被替换是有原因的。
如果您的用户说他们绝对想要 08, Aug 2019
,您需要通过格式模式字符串来指定:
DateTimeFormatter handBuiltFormatter = DateTimeFormatter.ofPattern("dd, MMM uuuu", englishIsrael);
String dateStringWeWant = date.format(handBuiltFormatter);
现在我们确实得到了您要求的输出:
08, Aug 2019
Link: Oracle tutorial: Date Time解释如何使用java.time,现代Java日期和时间API.