如何在安卓系统中将2021-07-06T19:27:46.811+0530格式转换为d MMM yyyy,hh:mm aaa格式

2022-08-24 00:00:00 datetime android kotlin simpledateformat

2021-07-06T19:27:46.811+0530->;当前值为字符串

我想将此格式转换为2021年5月7日06:45

提前谢谢


解决方案

您可以这样做

JAVA:

SimpleDateFormat parserFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
SimpleDateFormat convertFormat = new SimpleDateFormat("dd/MM/yyyy, hh:mm a", Locale.getDefault());
Date date = null;
try {
    date = parserFormat.parse("2021-07-06T19:27:46.811+0530");
    if (date != null) {
        String formatedDate = convertFormat.format(date);
        Log.e("formatted date",formatedDate);
    }
} catch (ParseException e) {
    e.printStackTrace();
    Log.e("formatted date",e.getMessage());
}

科特林:

val parserFormat =
        SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault())
val convertFormat =
        SimpleDateFormat("dd/MM/yyyy, hh:mm a", Locale.getDefault())
var date: Date? = null
try {
     date = parserFormat.parse("2021-07-06T19:27:46.811+0530")
     if (date != null) {
        val formatedDate = convertFormat.format(date)
        Log.e("formatted date", formatedDate)
     }
} catch (e: ParseException) {
    e.printStackTrace()
    Log.e("formatted date", e.message!!)
}

相关文章