【问题标题】:How to convert 2021-07-06T19:27:46.811+0530 format to d MMM yyyy, hh:mm aaa this format in android如何在 android 中将 2021-07-06T19:27:46.811+0530 格式转换为 d MMM yyyy, hh:mm aaa 这种格式
【发布时间】:2021-07-05 14:40:17
【问题描述】:

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

我想转换为 05/07/2021, 06:45 am 这种格式

提前致谢

【问题讨论】:

标签: android datetime kotlin simpledateformat


【解决方案1】:

使用java.time:

import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

fun main() {
    val input = "2021-07-06T19:27:46.811+0530"
    // define a DateTimeFormatter for parsing your input
    val parser = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSx")
    // and another one for formatting your output
    val formatter = DateTimeFormatter.ofPattern("dd/MM/uuuu, hh:mm a")
    // then parse the input to an OffsetDateTime using the parser
    val converted: String = OffsetDateTime.parse(input, parser)
                                          // and output the same time differently
                                          .format(formatter)
    // output the result
    println(converted)
}

这段代码的输出是

06/07/2021, 07:27 PM

好的,这与示例中显示所需输出的值不完全一样,但我认为这里的全部内容都与格式有关。调整这些值需要更多的努力,包括对所需行为的简要描述。

【讨论】:

  • 这是一个很好的答案,而且确实是人们应该更喜欢使用的答案。我认为问题很明确,希望给出 format,而不是那个 value,所以我认为我们已经完成了。
【解决方案2】:

你可以这样做

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!!)
}

【讨论】:

    【解决方案3】:

    你可以试试这个:

    //Formats the date according to the given pattern
        private fun dateFormat(date: Date, pattern: String = "yyyy-MM-dd"): String =
            SimpleDateFormat(pattern, Locale.US).format(date)
    
        
    

    如果你必须:

    //string to date convert
            fun convertStringDate(dateString: String): LocalDate {
                val format = DateTimeFormatter.ISO_DATE
                val tmpDate = LocalDate.parse("2019-12-10", format)
                var parsedDate: LocalDate = tmpDate
                try {
                    parsedDate = LocalDate.parse(dateString, format)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                return parsedDate
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多