How to Get Current Date and Time in Java – Javatpoint

Get Current Date and Time in Java

There are many ways to get current the date and clock in Java. There are many classes that can be used to get current date and clock time in Java .

  1. java.time.format.DateTimeFormatter class
  2. java.text.SimpleDateFormat class
  3. java.time.LocalDate class
  4. java.time.LocalTime class
  5. java.time.LocalDateTime class
  6. java.time.Clock class
  7. java.util.Date class
  8. java.sql.Date class
  9. java.util.Calendar class

Get Current Date and Time: java.time.format.DateTimeFormatter

The LocalDateTime.now ( ) method acting returns the example of LocalDateTime class. If we print the example of LocalDateTime class, it prints the stream date and prison term. To format the current date, you can use DateTimeFormatter class which is included in JDK 1.8 .
FileName: CurrentDateTimeExample1.java

Output:

2017/11/06 12:11:58

Get Current Date and Time: java.text.SimpleDateFormat

The SimpleDateFormat class is besides used for formatting date and time. But it is honest-to-god approach .
FileName: CurrentDateTimeExample2.java

Output:

06/11/2017 12:26:18

Get Current Date: java.time.LocalDate

The LocalDate.now ( ) method acting returns the example of the LocalDate class. If we print the exemplify of the LocalDate class, it prints the current date .
FileName: CurrentDateTimeExample3.java

Output:

2021-12-17

Get Current Time: java.time.LocalTime

The LocalTime.now ( ) method returns the case of LocalTime class. If we print the example of LocalTime class, it prints the stream time .
FileName: CurrentDateTimeExample4.java

Output:

15:55:10.424178667

Get Current Date & Time: java.time.LocalDateTime

The LocalDateTime.now ( ) method returns the case of LocalDateTime class. If we print the case of LocalDateTime class, it prints the current go steady and prison term .
FileName: CurrentDateTimeExample5.java

Output:

2021-12-17T15:59:19.516010365

Get Current Date & Time: java.time.Clock

The Clock.systemUTC ( ) .instant ( ) method returns the stream date and time both.

FileName: CurrentDateTimeExample6.java

Output:

2021-12-17T16:04:03.930224479Z

Get Current Date & Time: java.util.Date

By printing the exemplify of java.util.Date class, you can print the stream date and time in Java. There are two ways to do so .
1st way:
FileName: CurrentDateTimeExample7.java

Output:

Fri Dec 17 16:07:15 GMT 2021

2nd way:
FileName: CurrentDateTimeExample8.java

Output:

Fri Dec 17 16:07:15 GMT 2021

Get Current Date: java.sql.Date

By printing the case of java.sql.Date class, you can print current date in Java. It does n’t print time. This date example is broadly used to save the current date in the database .
FileName: CurrentDateTimeExample9.java

Output:

2021-12-17

Get Current Date & Time: java.util.Calendar

The Calendar class can be used to get the example of the Date class. The getTime ( ) method acting of the Calendar course returns the exemplify of java.util.Date. The Calendar.getInstance ( ) method returns the example of the Calendar class .
FileName: CurrentDateTimeExample10.java

Read more : How to Prune Tomatoes

Output:

Fri Dec 17 19:23:10 GMT 2021

Note: It is recommended to use the Calendar class for getting the current date and time in classical Date API. Since Java 8, you can use LocalDate, LocalTime, or LocalDateTime classes.

adjacent topicJava Convert String to int

source : https://thaitrungkien.com
Category : Tutorial

Related Posts

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *