如何在 Kotlin 中获取当前本地日期和时间

153

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

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

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


5
如果您的API级别低于26,只需将ThreeTenABP库添加到您的项目中,导入org.threeten.bp.LocalDateTime,然后执行您尝试过的操作:LocalDateTime.now(yourTimeZone)(我建议指定时区,但如果您不指定,将使用JVM时区设置)。更多信息请参见此问题:如何在Android项目中使用ThreeTenABP - Ole V.V.
请查看以下链接以获取您所需的解决方案:https://dev59.com/3m865IYBdhLWcg3wkfcW#57674721 - Prashant Jajal
14个回答

105

试一试:

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

29
日期(Date)类已被大多数废弃,多年来一直在逐步淘汰。我不建议初学者使用该类。 - Prime624
4
新手上路。Date 的替代方案是什么? - Clément Cardonnel
1
@ClémentCardonnel 只需使用 calendar.getInstance() 获取的日期。 - Sourav Emon

61

我使用 Calendar 编写了一个在 minSdkVersion < 26 时获取当前日期时间的实用方法。

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")

5
这应该是被接受的答案:简单干净,我将其作为包级别函数实现(关于包级别函数:https://dev59.com/1FkT5IYBdhLWcg3wT91f),以便在任何地方都可以调用它。 - oneManArmin

56

33
一个例子会很好。 - barrypicker

22

您可以从日历实例中获取当前的年份、月份、日期等信息。

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, ... )

11

试试这个:

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),比如对于ASCII日期可以使用Locale.US。

前三个选项需要API 29或以上版本。


7
获取当前日期的 Kotlin 代码如下所示:
val dateNow = Calendar.getInstance().time

7
fun main(){
println(LocalDateTime.now().toString()) //2021-10-25T12:03:04.524
println(Calendar.getInstance().time) //Mon Oct 25 12:02:23 GST 2021
}

以下是上述选项,其中包括输出内容的注释。

返回 LocalDateTime.now().toString() 需要 Android O。 - Sattar

6
你可以使用这个函数。
fun getCurrentDate():String{
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
return sdf.format(Date())
}

2
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())

1
目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

1
假设我们获得了以秒为单位的时间,我们可以这样做:(kotlin语言)
val dateObject = Date(timeInMillis)
val calendarInstance = Calendar.getInstance()
calendarInstance.time = dateObject
val hour = calendarInstance.get(Calendar.HOUR)
val minute = calendarInstance.get(Calendar.MINUTE)
val ampm = if(calendarInstance.get(Calendar.AM_PM)==0) "AM " else "PM "
val date = calendarInstance.get(Calendar.DATE)
val month = calendarInstance.get(Calendar.MONTH)
val year = calendarInstance.get(Calendar.YEAR)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接