将日期月份格式化为3个首字母的字符串-Kotlin [英] Format month of date to string of 3 first letters- Kotlin

查看:125
本文介绍了将日期月份格式化为3个首字母的字符串-Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期为"08/08/2019",我希望它看起来像这样:"08,Aug 2019",我尝试使用when,但想知道是否有更简单的方法吗?我知道这是一个很小的问题,但是我试图在互联网上找到答案,但找不到.

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.

推荐答案

首先,您需要将字符串转换为Date对象,然后使用新的

first, you need to convert the string to Date object then convert it to your format using the new 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

这篇关于将日期月份格式化为3个首字母的字符串-Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆