如何在 Java 9 中的 JShell 中获取日期和时间?
javaobject oriented programmingprogramming
JShell 是一个交互式命令行工具,可以让我们学习、研究和探索 Java 语言及其 API。我们可以在控制台中输入任何有效的 Java 代码并立即获得结果,而无需使用 main() 方法编写详细类 。
如果我们想在 JShell 中获取带有时间的当前日期,请使用以下代码片段。
C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> new Date() $1 ==> Fri Feb 28 11:59:23 IST 2020 jshell>
在下面的代码片段中,我们需要获取一个带有毫秒数的日期。
jshell> new Date().getTime() $2 ==> 1582871487654 jshell> System.currentTimeMillis() $3 ==> 1582871513421 jshell> new Date(1582871513421L) $4 ==> Fri Feb 28 12:01:53 IST 2020 jshell>
在下面的代码片段中,我们需要获取时间。
jshell> java.time.Instant.now() $5 ==> 2020-02-28T07:07:33.941720100Z jshell>