scala - 如何在 Scala 中获取当前时间戳作为没有空格的字符串?

标签 scala

例如,我想获取当前时间的字符串:“20180122_101043”。我怎样才能做到这一点?

我可以创建一个 val cal = Calendar.getInstance()但我不确定之后该怎么办。

最佳答案

LocalDateTime是你可能想要使用的,

scala> import java.time.LocalDateTime
import java.time.LocalDateTime

scala> LocalDateTime.now()
res60: java.time.LocalDateTime = 2018-01-22T01:21:03.048

如果你不想默认 LocalDateTime格式基本上是 ISO format没有区域信息,您可以申请DateTimeFormatter如下,
scala> import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatter

scala> DateTimeFormatter.ofPattern("yyyy-MM-dd_HH:mm").format(LocalDateTime.now)
res61: String = 2018-01-22_01:21

相关资源 - How to parse/format dates with LocalDateTime? (Java 8)

关于scala - 如何在 Scala 中获取当前时间戳作为没有空格的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48378006/

相关文章:

scala - 如何定义扩展此特征的任何内容

案例类上的 Scala 断言未知参数

scala - Spark 数据集唯一 id 性能 - row_number 与 monotonically_increasing_id

scala - scala 2.10.x 中的隐式解析。这是怎么回事?

scala - Scala中的泛型不变量协变对变量

scala - 是否有一个函数可以分别采用两个变换函数来变换/映射 Either 的 Left 和 Right 情况?

scala - 如何函数式地编写 Scala 的 Either

scala - sbt 程序集中的合并策略和缺少应用程序加载程序

scala - 向部分函数添加新参数

scala - 在 Scala 中,我可以通过隐式转换引用私有(private)类型