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 to analyze the principle of Java date and time Class

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about how to analyze the principles of Java date and time. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

This article mainly introduces the principle analysis of Java date and time class, which is introduced in great detail through the sample code, which has a certain reference value for everyone's study or work. Friends who need it can refer to it.

Basic knowledge

Date: similar to 2018-12-12 time: similar to 2018-12-12 12:12:12 time: similar to 2018-12-12 12:12:12 area: Locale in the computer, such as zh_CN, en_US, etc., which affects the display of date, time, currency, etc. EpochTime: seconds from January 1, 1970 UTC+00:00 to the present (not milliseconds) It can be roughly considered to be equivalent to TimeStamp: there are 24 time zones in the world, London (GMT+00:00) is the standard time zone GMT and UTC can be roughly considered to be equivalent, the time represented by GMT and UTC is not affected by daylight saving time, while the time expressed in regions (such as America/New_York) is affected by daylight saving time are stored in computers as EpochTime (that is, TimeStamp), so computers all over the world are the same. However, because of the different settings of the computer region, it is displayed as Date and Calendar at different times.

The usage of Date

/ / get a Date object, that is, Datedate = new Date (); / / get TimeStamplong timestamp = date.getTime (); / / timestamp generate Datedate = new Date (timestamp); / / convert the string String dateString = date.toString () to the default format; / / convert SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); dateString = simpleDateFormat.format (date) / / parse to Datedate = simpleDateFormat.parse (dateString) according to the specified format string

The usage of Calendar

/ / get a CalendarCalendar calendar = Calendar.getInstance (); / / get the day of the month int day = calendar.get (Calendar.DAY_OF_MONTH); / / set to the specified time calendar.set (Calendar.DAY_OF_MONTH, 1); / / convert to DateDate date = calendar.getTime (); / / convert to timestamplong timestamp = calendar.getTimeInMillis ()

The usage of LocalDateTime

/ / get a LocalDateTimeLocalDateTime localDateTime = LocalDateTime.now (); / / get a specified LocalDateTime. Note that the month starts at 1 instead of 0, so December uses 12localDateTime = LocalDateTime.of (2018, 12, 12, 21, 36, 36); / / LocalDateTime is formatted into the specified format DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss"); String localDateTimeString = dateTimeFormatter.format (localDateTime) / / parse LocalDateTime according to the specified format. Note that DateTimeFormatter enters localDateTime = LocalDateTime.parse (localDateTimeString, dateTimeFormatter) as a parameter; / / minus one day, you need to note that the object is immutable localDateTime = localDateTime.minusDays (1); / / the hour is set to 12:00 localDateTime = localDateTime.withHour (12); / / set to the first day of the month localDateTime = localDateTime.withDayOfMonth (1) / / set to the last day of the month, note that here is TemporalAdjusters instead of TemporalAdjusterlocalDateTime = localDateTime.with (TemporalAdjusters.lastDayOfMonth ()); / / determine the sequence of two LocalDateTime boolean before = localDateTime.isBefore (LocalDateTime.now ()); / / calculate the number of days Period period = LocalDate.now (). Until (LocalDate.of (2019, 1, 1)); long days = period.getDays () / / calculate the number of days between two LocalDateTime, which may be negative days = localDateTime.until (LocalDateTime.now (), ChronoUnit.DAYS); / / the number of days away from EpochTime days = LocalDate.now () .toEpochDay (); / / LocalDateTime cannot be converted to timestamp because it has no concept of time zone

ZonedDateTime is equivalent to LocalDateTime plus a ZoneId.

Instant can be understood as TimeStamp, which is equivalent to a wrapper class of Long

/ / get ZonedDateTimeZonedDateTime zonedDateTime = LocalDateTime.now () .atZone (ZoneId.of ("Asia/Shanghai")) from LocalDateTime; / / get ZonedDateTimezonedDateTime = Instant.now () .atZone (ZoneId.systemDefault ()) from Instant; / / get the specified Instant. Note that System.currentTimeMillis () returns milliseconds, while TimeStamp is second Instant instant = Instant.ofEpochSecond (System.currentTimeMillis () / 1000). / / TimeStamp get LocalDateTimeLocalDateTime localDateTime = LocalDateTime.ofEpochSecond (/ / get the current timestamp (timestamp) instant.getEpochSecond (), / / millisecond 0, / / get the ZoneOffset of the system, instant zone offset OffsetDateTime.now (). GetOffset (); / / week-related operations LocalDate now = LocalDate.now (); LocalDate sundayDate = now.with (DayOfWeek.SUNDAY); int sundayValue = sundayDate.getDayOfWeek (). GetValue ()

The above is the editor for you to share how to analyze the Java date and time class principle, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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