简体   繁体   English

Kotlin Android / Java 字符串日期时间格式,API21

[英]Kotlin Android / Java String DateTime Format, API21

I want convert string datetime to formatted string.我想将字符串日期时间转换为格式化字符串。 eg "2018-12-14T09:55:00" to "14.12.2018 09:55" as String => Textview.text例如“2018-12-14T09:55:00”到“14.12.2018 09:55”作为字符串 => Textview.text

how can I do this with kotlin or java for android?对于 android,我如何使用 kotlin 或 java 执行此操作?

Parse it to LocalDateTime then format it:将其解析为LocalDateTime然后对其进行格式化:

LocalDateTime localDateTime = LocalDateTime.parse("2018-12-14T09:55:00");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
String output = formatter.format(localDateTime);

If this does not work with api21, you can use:如果这不适用于 api21,您可以使用:

SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");
String output = formatter.format(parser.parse("2018-12-14T09:55:00"));

or import ThreeTenABP.或导入 ThreeTenABP。

Kotlin API levels 26 or greater: Kotlin API 级别 26 或更高:

val parsedDate = LocalDateTime.parse("2018-12-14T09:55:00", DateTimeFormatter.ISO_DATE_TIME)
val formattedDate = parsedDate.format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm"))

Below API levels 26: API 级别 26 以下:

val parser =  SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
val formatter = SimpleDateFormat("dd.MM.yyyy HH:mm")
val formattedDate = formatter.format(parser.parse("2018-12-14T09:55:00"))

If you have a date-time that represents a value in a specific time zone but the zone is not encoded in the date-time string itself (eg, "2020-01-29T09:14:32.000Z") and you need to display this in the time zone you have (eg, CDT)如果您有一个表示特定时区中的值的日期时间,但该区域未在日期时间字符串本身中编码(例如,“2020-01-29T09:14:32.000Z”)并且您需要显示这是您拥有的时区(例如,CDT)

val parsed = ZonedDateTime.parse("2020-01-29T09:14:32.000Z", DateTimeFormatter.ISO_DATE_TIME).withZoneSameInstant(ZoneId.of("CDT"))

That parsed ZoneDateTime will reflect the time zone given. parsed ZoneDateTime 将反映给定的时区。 For example, this date would be something like 28 Jan 2020 at 8:32am.例如,此日期类似于 2020 年 1 月 28 日上午 8:32。

In kotlin u can do this way to format string to date :-在 kotlin 中,您可以通过这种方式将字符串格式化为日期:-

val simpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.getDefault())
val date = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault()).format(simpleDateFormat.parse("01-02-2022 14:23:05")!!)

Should import java.text.SimpleDateFormat For SimpleDateFormat Class to work on api 21应该导入 java.text.SimpleDateFormat 以便 SimpleDateFormat 类在 api 21 上工作

Here is Kotlin One Liner,It will return empty string if input date cannot be parsed.这里是 Kotlin One Liner,如果无法解析输入日期,它将返回空字符串。

fun String.getDateInAnotherFormat(inputFormat: String,outputFormat: String):String = SimpleDateFormat(inputFormat, Locale.getDefault()).parse(this)?.let { SimpleDateFormat(outputFormat,Locale.getDefault()).format(it) }?:""

Usage:用法:

var dateStr = "2000-12-08"
dateStr.getDateInAnotherFormat("yyyy-MM-dd","MMM dd YYYY")

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

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