我有一个日期时间字符串,带有时区,例如" 2019-05-21 04:49:39.000Z"。我如何在没有拆分方法的情况下从该字符串中拆分日期。我必须使用时区格式化器。任何人请帮助我。
public static String getDateTime(String create_on({
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat outFormat = new SimpleDateFormat("dd MMM yyyy hh:mm aa");
try {
inFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date value = inFormat.parse(created_on);
outFormat.setTimeZone(TimeZone.getDefault());
created_on = outFormat.format(value);
} catch (ParseException e) {
e.printStackTrace();
}
return created_on;
}
使用此功能。它将帮助您获得日期和时间
使用一些库将字符串解析为例如。OffsetDateTime,从parseddatetime变量获得任何部分(小时,最小...(之后在Kotlin(Min API 26(中:
val parsedDateTime = OffsetDateTime
.parse("2019-05-21 04:49:39.000Z", DateTimeFormatter.ISO_DATE_TIME)
val time = "${parsedDateTime.hour} : ${parsedDateTime.minute}"
检查此链接以获取更多示例:示例
https://grokonez.com/kotlin/kotlin-convert-string-datetime
更新:
作为Ole V.V.在评论中提到 - 如果您尚未在API级别26上,则可以将Threetenabp添加到Android项目中,并从那里导入OffsetDateTime(并且相同的代码适用于较低的API(。