In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to understand Java 8 time API, the content is concise and easy to understand, absolutely can make your eyes shine, through the detailed introduction of this article I hope you can gain something.
Note that Java 8 preview will include new APIs for date and time (compliant with JSR 310 specification). Examples of these new APIs are provided. First of all, we will introduce the following: Instant, LocalDate, LocalTime and LocalDateTime.
Instant (java.time.Instant)
Perhaps the easiest way to get started with the java.time package is to look at the Instant class. The so-called Instant class represents a certain time (a bit like java.util.Date), which is accurate to nanoseconds (rather than milliseconds like the old version of Date). If you use nanoseconds to represent a time, then the original use of a one-bit Long type is not enough, it needs to take up a little more storage space, but in fact it consists of two Long fields, *** part stores the number of seconds since the standard Java computing era (that is, January 1, 1970) to the present, and the second part stores the nanoseconds (never more than 999,999,999). Let's look at the following concrete example:
//Get the current time
//Get the current time
Instant instant = Instant.now();
//Output in ISO-8601
System.out.println(instant);
Running the above code in Open JDK will produce output in the following format: 2013-06-25T16:22:52.966Z, you can see that a more precise date has been entered. The following example is more about the use of Instant class, such as
//Convert java.util.Date to Instant instant = Instant.ofEpochMilli(new Date().getTime()); //Create Instant type instant = Instant.parse("1995-10-23T10:12:35Z") from string type;
Note that in the example above, there is a string that creates the Instant type of time, but Instant represents a time and does not include the concept of time zone, so it must be passed in a string that conforms to UTC format.) The Instant API also provides some useful ways to perform operations using classes in Instant and other packages. Here are some examples:
instant.plus(Duration.ofHours(5).plusMinutes(4));
Add five hours and four minutes to the present time.
How many java.time.Instant instances are used in this example? The answer is two. Java.time This package is thread-safe and immutable like most other classes. Instant also obeys this rule, so the plus method produces a new instance, such as:
Instant instant1 = instant.plus (Duration.ofHours(5).plusMinutes(4)); System.out.println("Instant is immutable, so instant==instant returns: " + (instant == instant1)); Output is:
Instant is immutable, so instant==instant returns: false
Here are more examples of calculations:
//Subtract 5 days of an instant Calculate 5 days ago instant.minus(5, ChronoUnit.DAYS); // Option 1 Method 1 instant.minus(Duration.ofDays(5)); // Option 2 Method 2 //Calculate minutes between two instants long diffAsMinutes = instant.periodUntil(instant1, ChronoUnit.MINUTES); //Method 1 long diffAsMinutes = ChronoUnit.MINUTES.between(instant, instant1); //Method 2
Instant is comparable, meaning that two Instant can be compared. It provides isAfter() and isBefore() methods for comparison, as shown in the following code:
//compare System.out.format("instant1.compareTo(instant)=%d.% n", instant1.compareTo(instant)); //use isAfter() and isBefore() System.out.format("instant1.isAfter(instant)=%b, instant1.isBefore(instant)=%b.% n", instant1.isAfter(instant), instant1.isBefore(instant)) Output result:
instant1.compareTo(instant)=1. instant1.isAfter(instant)=true, instant1.isBefore(instant)=false
Localdate and LocalTime
LocalDate represents a date without a time zone, such as 1-1-2000. LocalTime represents time without a time zone, such as 04:44:50.12, unlike the Instant class mentioned earlier, which calculates an offset from 1970. The output of these two classes is the date and time that people are accustomed to reading. There are many ways to get instances of LocalTime and LocalDate, such as
LocalDate localDate = LocalDate.now();
localDate = LocalDate.ofYearDay(2005, 86); //Get 86th day of 2005 (27-Mar-2005)
localDate = LocalDate.of(2013, Month.AUGUST, 10); //August 10, 2013
LocalTime localTime = LocalTime.of(22, 33); //10:33 PM
localTime = LocalTime.now();
localTime = LocalTime.ofSecondOfDay(4503); //Returns the 4503rd second of the day (1:15:30 AM)
LocalDate and LocalTime obey the same threading rules as Instant--i.e., their real instances are immutable. LocalDate and LocalTime have the same calculation and comparison methods as Instant (some of which are defined in the java.time.temporal.Temporal interface and implemented by the classes above):
LocalDate localDate1 = localDate.plus(5, ChronoUnit.HOURS); localDate.isBefore(localDate1);
LocalDateTime
*** Take a look at the most important one in the simple date and time class:LocalDataTeime. It is a combination of LocalDate and LocalTime, representing date and time without time zone. LocalDateTime looks a lot like Instant, but remember that Instant is an instant point in time without a time zone. Some people may say that the instant time point is not the date + time? It looks like this, but there are still differences. For example, LocalDateTime may be just a simple concept of date and time for users. Consider the following example: Both were born at 11:00 on July 2, 2013, *** one in England and the second in California, and if we ask when they were born, they both appear to have been born at the same time. But if we look closely at the timeline (e.g. Greenwich Time), we find that people born in the UK are a few hours later than people born in the UK (this is what Instant means and converts it to UTC). Other than that, LocalDateTime usage is similar to the other classes described above, as shown in the following example:
LocalDateTime localDateTime = LocalDateTime.now ();//Current time plus 25 hours and 3 minutes LocalDateTime inTheFuture = localDateTime.plusHours(25).plusMinutes(3); //can also be used in localTime and localDate System.out.println (localDateTime.toLocalTime().plusHours(25).plusMinutes(3)); System.out.println (localDateTime.toLocalDate().plusMonths(2)); //You can also use the Duration and Period classes that implement the TemportalAmount interface System.out.println (localDateTime.toLocalTime().plus(Duration.ofHours(25).plusMinutes(3))); System.out.println(localDateTime.toLocalDate().plus(Period.ofMonths(2))); That's how to understand Java 8's Time API. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.