
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the date and time in JShell in Java 9?
In this article, we will learn to get the date and time in JShell in Java 9. First, we will learn about the date and time class in Java with an example, and after that will learn about date and time in JShell with multiple examples.
JShell
JShell is an interactive command-line tool that allows us to learn, investigate, and explore the Java language and its 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.
C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell>
Getting Date and Time in JShell
We can work with dates and times in JShell as we work with them in Java programming. The java.time package is already present in the JShell environment.
Example 1
If we want to get the current date with time in JShell by using the below code snippet, using the Date() class.
jshell> new Date() $1 ==> Fri Feb 28 11:59:23 IST 2020
Example 2
In the below code snippet, we need to get a date with a number of milliseconds using the getTime() or currentTimeMillis().
jshell> new Date().getTime() $2 ==> 1582871487654 jshell> System.currentTimeMillis() $3 ==> 1582871513421 jshell> new Date(1582871513421L) $4 ==> Fri Feb 28 12:01:53 IST 2020
Example 3
In the below code snippet, we need to get the time using the Instant.now() method.
jshell> java.time.Instant.now() $5 ==> 2020-02-28T07:07:33.941720100Z jshell>