Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java get the current date and time

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report