@SuppressLint("DefaultLocale") @RequiresApi(Build.VERSION_CODES.O) private fun CalculationTime(){ val dt11 = System.currentTimeMillis() // 获取当前时间(毫秒) while (true){ val dt21 = System.currentTimeMillis() // 再次获取当前时间(毫秒) val durationMillis = dt21 - dt11 // 计算时间差(毫秒) // 将毫秒转换为TimeSpan并格式化为 "时:分:秒" val ts1 = java.time.Duration.ofMillis(durationMillis) // Kotlin使用java.time包来处理时间 val formattedTime = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { String.format("% 02d:% 02d:% 02d",//注意%与02 中间不能有空格 ts1.toHoursPart(), ts1.toMinutesPart() % 60, // 获取分钟部分,不包括小时转换后的分钟 ts1.toSecondsPart() % 60 // 获取秒部分,不包括分钟转换后的秒 ) } else { TODO("VERSION.SDK_INT < S") } // 打印结果 println("lqwvje,耗时"+formattedTime) //输出 lqwvje,耗时00:00:05 Thread.sleep(1000) } }
本文来自 www.LuoFenMing.com