如何在 Scala 中获取当前时间戳作为没有空格的字符串? [英] How to get current timestamp in Scala as a string without spaces?

查看:34
本文介绍了如何在 Scala 中获取当前时间戳作为没有空格的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取例如当前时间的字符串:20180122_101043".我该怎么做?

I want to get for example a string of the current time: "20180122_101043". How can I do this?

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

I can create a val cal = Calendar.getInstance() but I'm not sure what to do with it after.

推荐答案

Calendar 在这里不是最佳选择.使用:

Calendar is not the best choice here. Use:

  • java.util.Date + java.text.SimpleDateFormat 如果你有 java 7 或更低版本

  • java.util.Date + java.text.SimpleDateFormat if you have java 7 or below

new SimpleDateFormat("YYYYMMdd_HHmmss").format(new Date)

这篇关于如何在 Scala 中获取当前时间戳作为没有空格的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆