ホームページ  >  記事  >  Java  >  Java 9でJShellを使用して日付と時刻を取得するにはどうすればよいですか?

Java 9でJShellを使用して日付と時刻を取得するにはどうすればよいですか?

PHPz
PHPz転載
2023-08-20 10:21:02958ブラウズ

如何在Java 9中使用JShell获取日期和时间?

JShell is an interactive command-line tool that can allow us to learn, investigate, and explore the Java language and their API. We can type any valid java code into the console and get immediate results without the need to write a verbose class with the main() method.

If we want to get the current date with time in JShell by using the below code snippet.

<strong>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></strong>

在下面的代码片段中,我们需要获取一个日期毫秒数

<strong>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></strong>

In the below code snippet, we need to get the time.

<strong>jshell> java.time.Instant.now()
$5 ==> 2020-02-28T07:07:33.941720100Z

jshell></strong>

以上がJava 9でJShellを使用して日付と時刻を取得するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。