Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Java get the current date and time

Shulou Source: shulou.com Published: 2022-06-01 13:38:59 10月01日 Update

This article will explain in detail how Java obtains the current date and time. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. System.currentTimeMillis ()

The standard time can be obtained by using the System.currentTimeMillis () method, which has the advantage that it is not affected by the time zone, but the result is in the format of the timestamp, such as:

1543105352845

You can convert the timestamp into a format that we can understand through code:

SimpleDateFormat formatter= new SimpleDateFormat ("yyyy-MM-dd 'at' HH:mm:ss z"); Date date = new Date (System.currentTimeMillis ()); System.out.println (formatter.format (date))

Then the time corresponding to the timestamp is:

2018-11-25 at 01:22:12 CET

[note] this method returns the current value based on our system time, because time zones vary around the world.

2 、 Java.util.Date

In Java, one of the easiest ways to get the current date is to directly instantiate the Date class located in the Java package Java.util.

Date date = new Date (); / / this object contains the current date value

The date obtained above can also be converted to the format we need through format, such as:

SimpleDateFormat formatter = new SimpleDateFormat ("dd-MM-yyyy HH:mm:ss"); System.out.println (formatter.format (date))

3 、 Calendar API

The Calendar class is designed to convert dates and times between specific times and calendar fields.

Use Calendar to get the current date and time:

Calendar calendar = Calendar.getInstance (); / / get current instance of the calendar

Like date, we can convert the format we need through format:

SimpleDateFormat formatter = new SimpleDateFormat ("dd-MM-yyyy HH:mm:ss"); System.out.println (formatter.format (calendar.getTime ()

Print the results:

25-11-2018 00:43:39

4 、 Date/Time API

Java 8 provides a completely new API to replace Java.util.Date and Java.util.Calendar.

Date/Time API provides several classes to help us get the job done, including the following:

LocalDate

LocalTime

LocalDateTime

ZonedDateTime

4.1 LocalDate

LocalDate generally shows only the date, not the time. This means that we can only get the current date, but not the specific time of the day.

LocalDate date = LocalDate.now (); / / get the current date

We can convert it through format:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("dd-MM-yyyy"); System.out.println (date.format (formatter))

The following results are obtained, but only for the year, month and day:

25-11-2018

4.2 LocalTime

LocalTime, in contrast to LocalDate, can only get the time, not the date.

LocalTime time = LocalTime.now (); / / get the current time

Format in the following ways:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("HH:mm:ss"); System.out.println (time.format (formatter))

Get the result, only time, no date:

00:55:58

4.3 LocalDateTime

As the name implies, LocalDateTime connects the above two, that is, you can get both the date and the time. Therefore, LocalDateTime is the most commonly used Date/Time class in Java.

LocalDateTime dateTime = LocalDateTime.now (); / / get the current date and time

The same format method:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("dd-MM-yyyy HH:mm:ss"); System.out.println (dateTime.format (formatter))

The results are obtained:

25-11-2018 00:57:20

4.3 ZonedDateTime

ZoneDateTime's class in Java 8 date time API represents date and time time zone information. ZonedDateTime is generally immutable, which means that all methods of performing calculations on objects of this class return a new ZonedDateTime instance:

Creation method:

ZonedDateTime ZonedDateTime = ZonedDateTime.now (); this is the end of the article on "how to get the current date and time by Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Time date result can be passed method format way time zone article content only example meaning more help different good practical as the name implies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information OPPO Reno Docker Linux Microsoft