【问题标题】:How to get current local date and time in Kotlin如何在 Kotlin 中获取当前的本地日期和时间
【发布时间】:2018-04-10 21:38:23
【问题描述】:

如何在 Kotlin 中以当地时间获取当前日期(日月和年)和时间(小时、分钟和秒)?

我尝试通过LocalDateTime.now(),但它给了我一个错误说Call requires API Level 26 (curr min is 21)

如何在 Kotlin 中获取时间和日期?

【问题讨论】:

标签: android datetime kotlin


【解决方案1】:

试试这个:

 val sdf = SimpleDateFormat("dd/M/yyyy hh:mm:ss")
 val currentDate = sdf.format(Date())
 System.out.println(" C DATE is  "+currentDate)

【讨论】:

  • Date 大部分已被弃用,并且多年来一直处于逐步淘汰的过程中。我不同意向新学习者推荐它。
【解决方案2】:

java.util.Calendar.getInstance() 表示使用当前语言环境和时区的当前时间。

您还可以选择导入和使用 Joda-Time 或 forks for Android 之一。

【讨论】:

  • 举个例子就好了。
【解决方案3】:

当我们的 minSdkVersion Calendar 获取当前日期时间的 utils 方法。

fun Date.toString(format: String, locale: Locale = Locale.getDefault()): String {
    val formatter = SimpleDateFormat(format, locale)
    return formatter.format(this)
}

fun getCurrentDateTime(): Date {
    return Calendar.getInstance().time
}

使用

import ...getCurrentDateTime
import ...toString
...
...
val date = getCurrentDateTime()
val dateInString = date.toString("yyyy/MM/dd HH:mm:ss")

【讨论】:

【解决方案4】:

您可以从日历实例中获取当前年、月、日等

val c = Calendar.getInstance()

val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)

val hour = c.get(Calendar.HOUR_OF_DAY)
val minute = c.get(Calendar.MINUTE)

如果您需要它作为 LocalDateTime,只需使用上面获得的参数创建它

val myLdt = LocalDateTime.of(year, month, day, ... )

【讨论】:

    【解决方案5】:

    试试这个:

    val date = Calendar.getInstance().time
    val formatter = SimpleDateFormat.getDateTimeInstance() //or use getDateInstance()
    val formatedDate = formatter.format(date)
    

    您也可以使用自己的模式,例如

    val sdf = SimpleDateFormat("yyyy.MM.dd")
    // 2020.02.02
    

    要获取本地格式,请使用getDateInstance()getDateTimeInstance()getTimeInstance(),或使用新的SimpleDateFormat(String template, Locale locale),例如Locale.US 用于ASCII 日期。 前三个选项需要 API 级别 29。

    【讨论】:

      【解决方案6】:

      要在 Kotlin 中获取当前日期,请执行以下操作:

      val dateNow = Calendar.getInstance().time
      

      【讨论】:

        【解决方案7】:

        你可以使用这个功能

        fun getCurrentDate():String{
        val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
        return sdf.format(Date())
        }
        

        【讨论】:

          【解决方案8】:

          查看这些易于使用的 Kotlin 日期格式扩展

          fun String.getStringDate(initialFormat: String, requiredFormat: String, locale: Locale = Locale.getDefault()): String {
              return this.toDate(initialFormat, locale).toString(requiredFormat, locale)
          }
          
          fun String.toDate(format: String, locale: Locale = Locale.getDefault()): Date = SimpleDateFormat(format, locale).parse(this)
          
          fun Date.toString(format: String, locale: Locale = Locale.getDefault()): String {
              val formatter = SimpleDateFormat(format, locale)
              return formatter.format(this)
          }
          

          【讨论】:

            【解决方案9】:
            fun main(){
            println(LocalDateTime.now().toString()) //2021-10-25T12:03:04.524
            println(Calendar.getInstance().time) //Mon Oct 25 12:02:23 GST 2021
            }
            

            有上述选项,输出添加为注释。

            【讨论】:

              【解决方案10】:
              fun now(): String {
                  return SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date())
              }
              
              
                  val currentYear = SimpleDateFormat("yyyy",Locale.getDefault()).format(Date())
                  val currentMonth = SimpleDateFormat("MM",Locale.getDefault()).format(Date())
                  val currentDay = SimpleDateFormat("dd",Locale.getDefault()).format(Date())
              

              【讨论】:

              • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
              【解决方案11】:

              另一种解决方案是在 build.gradle 中更改项目的 api 级别,这将起作用。

              【讨论】:

                【解决方案12】:

                我使用它每 20 秒从 API 获取数据

                 private fun isFetchNeeded(savedAt: Long): Boolean {
                        return savedAt + 20000 < System.currentTimeMillis()
                    }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2015-08-16
                  • 2018-03-04
                  • 1970-01-01
                  • 2021-09-26
                  • 2011-01-01
                  • 2010-10-03
                  • 2019-08-13
                  • 2017-10-05
                  相关资源
                  最近更新 更多