在Scala中,可以使用Java的时间日期库来添加时间戳格式化。以下是一种常见的方法:
"yyyy-MM-dd HH:mm:ss"
是时间戳的格式,你可以根据需要进行调整。LocalDateTime.now()
用于获取当前的日期和时间,format(formatter)
将其格式化为指定的格式。完整的示例代码如下:
import java.time.format.DateTimeFormatter
import java.time.LocalDateTime
object TimestampExample extends App {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
val timestamp = LocalDateTime.now().format(formatter)
println(timestamp)
}
这样,你就可以在Scala中添加时间戳格式化了。
领取专属 10元无门槛券
手把手带您无忧上云