简体   繁体   English

如何知道kotlin中字符串日期的模式

[英]How to know pattern of string date in kotlin

I want to convert in ZonedDateTime .我想转换ZonedDateTime How can I recognise pattern to pass in DateTimeFormatter.ofPattern(????????) .如何识别要传入DateTimeFormatter.ofPattern(????????)的模式。

val string = "May 9, 2020 8:09:03 PM"

private fun getFormatDate(date: String): ZonedDateTime {
     val dateTimeFormatter = DateTimeFormatter.ofPattern(????????)
     return ZonedDateTime.parse(date, dateTimeFormatter)
}

Many Thanks非常感谢

You can do:你可以做:

private fun getFormatDate(date: String): ZonedDateTime {
    val dateTimeFormatter =
        DateTimeFormatter
            .ofPattern("MMM d, yyyy h:mm:ss a")
            .withZone(ZoneId.systemDefault()) // or some other zone that you would like to use
            .withLocale(Locale.ENGLISH)
    return ZonedDateTime.parse(date, dateTimeFormatter)
}

Note that it is important that you do .withZone which specifies the zone that the ZonedDateTime is going to have, and also .withLocale , so that the parser interprets things like "May" and "PM" in the correct language.请注意,重要的是您执行.withZone指定ZonedDateTime将拥有的区域,以及.withLocale ,以便解析器以正确的语言解释诸如“May”和“PM”之类的内容。

Try It Online 在线试用

See all the pattern letters here 在这里查看所有的模式字母

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM