当前位置:网站首页>Date time API details
Date time API details
2022-07-02 06:48:00 【I think starfish_ ninety-eight】
List of articles
Java8 Previous date question
Java 8 Previous date time API The problem is
stay Java 8 before , Believe that you are java.util.Date 、java.util.Calendar 、java.util.GregoiranCalendar and java.text.SimpleDateFormat These four categories are very familiar with , They are used to deal with date 、 The calendar 、 Gregorian calendar 、 Date time format .
There are many traps and pits in these four classes , such as :
- Non-thread safety : None of these four categories are thread safe . Developers have to deal with multithreading concurrency when using these classes .
- Poor design : On the one hand, dates and date formats are distributed across multiple packages ; On the other hand ,java.util.Date The default date for is 1970 year 1 month 1 Japan , There is no unity . and Date Class also lacks methods for directly manipulating dates .
- Time zones are difficult to handle : Because of the poor design , Developers have to write a lot of code to deal with time zones .
- There are other questions , Such as Calendar Class months are calculated from zero, etc .
Java8 improvement
Facing all kinds of problems ,Java 8 Finally redesigned all dates and times 、 Calendar time zone related API. And put them all in the same place java.time Under bags and subpackages .
- New date time API It's thread safe . Not only is there no setter Method , And any change to an instance will return a new instance , Keep the original instance unchanged .
- New date time API There are a lot of ways , Used to modify parts of the date and time , And return a new instance .
Draw lessons from the third-party date time database joda A lot of advantages . - In terms of time zones , New date time API Introduced Domain ( domain ) The concept .
- meanwhile Java 8 And for the original complex API To recombine and split , It's divided into many categories .
About time and time zones
GMT and UTC
UTC And GMT Basically equivalent to , The error does not exceed 0.9 second . But in daily use ,GMT And UTC There is no difference between the function and accuracy of .
GMT, It's Greenwich mean time , It's world time .GMT When the sun crosses the Greenwich meridian ( Prime meridian ) Time of time . But because of the uneven rotation of the earth , Lead to GMT Imprecise , It is no longer used as world standard time .
UTC, The coordinated universal time . It's passing through the average sun ( In Greenwich mean time GMT Subject to )、 The new time scale after the correction of the earth's axis motion , With 「 second 」 International Atomic Time in units . To ensure UTC And GMT The difference is not more than 0.9 second , In case of need ( for example 1998-12-31T23:59:60Z) Will be in UTC Plus positive or negative leap seconds . Harmonizing the world time zone will use “Z” To express , Coordinating the world is also known as “Zulu time”.UTC Now used as world standard time .
The time zone
Time zone as a geographical concept , Express “ A region that adheres to a uniform time standard ”.
The earth rotates from west to East , The East sees the sun before the West , The time in the East is also earlier than that in the West . In order to unify the time of the world ,1884 According to the international longitude conference rules, the world is divided into 24 Time zone ( In the east 、 Xige 12 Time zone ). Rule England ( The former site of Greenwich Observatory ) Zero time zone ( The benchmark UTC), In the east 1-12 District , In the west 1-12 District , Beijing, China is in the East 8 District (UTC+08:00), So we'll be ahead of the benchmark - That's what we do in the morning 9 a.m. , It's morning in London 1 spot .
Unix Time stamp
In the computer Unix Time stamp , Use from 1970-01-01T00:00:00Z(Z It indicates that the UTC Time ) So far, the millisecond difference is used as the value of time , And remove the period of “ Leap second ”( for example 1998-12-31T23:59:60Z), This, of course, is to simplify the complexity of computer time operations .Unix In the time system , Fix it every day 86400 second , This time is absolutely public , It has nothing to do with time zones .
Java Medium Unix Time
Java Make sure : Every day 24 Hours 、 Every hour 60 branch 、 Every minute 60 second .
Java In order to get “ At present ” The way of time , Its underlying implementation is , All by java.lang.System.currentTimeMillis() Provided from UTC 1970-01-01T00:00:00 Millisecond count .java.lang.System.currentTimeMillis() As native Method , Its realization is related to JVM It's related to the machine ( Usually use NTP Keep the agreement updated ).
Java 8 Date time
Class diagram
java.time.temporal Provided interface :
- TemporalField: Date and time “ Field ”, for example :2017-11-18 Medium 18 “ God ”
- TemporalUnit: Time “ Company ”, for example :1 year 13 Days of 13 “ God ”
- TemporalAccessor:“ Time related ” Object's “ read-only ” Interface
- Temporal:“ Time related ” Object's “ Reading and writing ” Interface , Inherited from TemporalAccessor
- TemporalAdjuster:Temporal Type object “ Set up & adjustment ” The functional interface of
- TemporalAmount: Period of time
API
New time and date API One of the most important points in , It defines the basic concepts of time and date , For example date 、 Time 、 Instantaneous time 、 The duration of the 、 Time zone and time period . They are based on ISO8601 Calendar system , It's the civil calendar of the world , That's what we call the Gregorian calendar
java.time The package contains the following main classes :
- LocalDate: Indicates a date without time , such as :2016-10-20
- LocalTime: Time without date , such as :23:12:10
- LocalDateTime: Date time , such as :2016-10-20 23:14:21
- TemporalAduster : Time regulator
- TemporalAdjusters: Get the date, time, etc , Like the first day of the month 、 The last day of the year
- Duration: The duration of the , Calculate two “ Time ” The interval of
- Period: Date interval , Calculate two “ date ” The interval of
- Instant:Unix Time , It's a time stamp , such as 2018-01-14T02:20:13.592Z
- Clock: The clock , Get the instantaneous time in a certain time zone
- ZoneId: The time zone id, for example Asia/Shanghai etc.
- ZonedDateTime: Date time with time zone
- DateTimeFormatter: Time format
new API Distinguish various date and time concepts and use similar methods to define patterns , This similarity is very beneficial to API Learning from . Sum up the general method rules :
- of: Static factory method , Used to create instances
- now: Static factory method , Create instance with current time
- parse: Static factory method , Get object instance from string parsing
- get: Get part of the state of the object
- is: Check if something is true, For example, compare before and after time
- with: Returns a copy of the time date object whose partial state has changed ( One by one with Method , Parameter is TemporalAdjusters type )
- plus: Returns a copy of the time and Date object with an increased time
- minus: Returns a time date object copy with reduced time
- to: Convert the current time date object to another , Some of the states may be lost .
- at: Combine this object with another object , for example :date.atTime(time)
- format: Format the time and date as a string
Be careful
Finally, emphasize ,Java 8 China new time and date API Medium All classes are immutable and thread safe , Any modification will return a new instance , Before java.util.Date、Calendar as well as SimpleDateFormat None of these key classes are thread safe .
边栏推荐
猜你喜欢
uniapp引入本地字体
Linux MySQL 5.6.51 community generic installation tutorial
Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
Build learning tensorflow
Unexpected inconsistency caused by abnormal power failure; Run fsck manually problem resolved
Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
如何调试微信内置浏览器应用(企业号、公众号、订阅号)
Thread hierarchy in CUDA
Latex compiles Chinese in vscode and solves the problem of using Chinese path
微信小程序基础
随机推荐
Record RDS troubleshooting once -- RDS capacity increases dramatically
Blog directory of zzq -- updated on 20210601
20201002 vs 2019 qt5.14 developed program packaging
20201025 Visual Studio2019 QT5.14 信号和槽功能的使用
There are multiple good constructors and room will problem
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
Eggjs -typeorm 之 TreeEntity 实战
Asynchronous data copy in CUDA
CTF web practice competition
Browser scrolling for more implementations
Tensorrt command line program
unittest.TextTestRunner不生成txt测试报告
Latex compilation error I found no \bibstyle &\bibdata &\citation command
A preliminary study on ant group G6
Apt command reports certificate error certificate verification failed: the certificate is not trusted
FE - 微信小程序 - 蓝牙 BLE 开发调研与使用
pytest(2) mark功能
Summary of advertisement business bug replay
How to try catch statements that return promise objects in JS
pytest(1) 用例收集规则