The Java.time of Java8

Source: Internet
Author: User
Tags ibm developerworks

Java 8 Java.time Package Tags: Java Java8| Publish time: 2013-10-15 08:29 | CoderbeeShare to:Source: Http://coderbee.net Package Overview

The Java.time package, introduced in JDK8, provides the main APIs for dates, times, instances, and cycles.

The classes defined by the Java.time package represent the rules of the date-time concept, including instants, durations, dates, times, time-zones and periods. These are based on the ISO calendar system, which is followed by Gregorian rules.

All classes are immutable and thread-safe.

Introduction to some classes
    • LocalDateTime: Stores the date and time, such as: 2013-10-15T14:43:14.539 .

    • Localdate: A date is stored, such as: 2013-10-15 .

    • LocalTime: Stores the time, such as: 14:43:14.539 .

The classes above can be combined by the following classes:

    • Year: Represents years.

    • Month: Represents the months.

    • Yearmonth: Indicates the month.

    • MonthDay: Indicates the day of the month.

    • DayOfWeek: Stores the day of the week.

Examples of conversions between classes:

  LocalDateTime LocalDateTime = Localdatetime.now (); System.out.println ("LocalDateTime:" + localDateTime); Localdate localdate = Localdate.now (); System.out.println ("localdate:" + localdate); LocalTime localtime = Localtime.now (); System.out.println ("localtime:" + localtime);//Gets the current year years = Year.now (); System.out.println ("Year:" + year);//Get Localdatelocaldate localDate1 = Year.atday (59) from year; System.out.println ("localDate1:" + localDate1);//link localtime to a localdate get a localdatetimelocaldatetime localDateTime1 = Localtime.atdate (localDate1); System.out.println ("localDateTime1:" + localDateTime1);//Get a yearyear year1 = Year.of with the specified year (2012); System.out.println ("year1:" + year1);//Get Yearmonthyearmonth Yearmonth = Year1.atmonth from year (2); System.out.println ("Yearmonth:" + yearmonth);//Yearmonth Specify day to get localdatelocaldate LocalDate2 = yearmonth.atday (29); System.out.println ("LocalDate2:" + localDate2);//Judging whether it is a leap year System.out.println ("Isleapyear:" + localdate2.isleapyear () );//February date for automatic processing of leap yearsCreate a Monthdaymonthday MonthDay = Monthday.of (2, 29); Localdate leapyear = monthday.atyear (2012); System.out.println ("leapyear:" + leapyear);//The same monthday is associated to another year localdate nonleapyear = Monthday.atyear (2011); System.out.println ("nonleapyear:" + nonleapyear);

The output of the above code is:

Localdatetime:2013-10-15t15:11:57.489localdate:2013-10-15localtime:15:11:57.489year:2013localdate1 : 2013-02-28localdatetime1:2013-02-28t15:11:57.489year1:2012yearmonth:2012-02localdate2:2012-02-29isleapyear: Trueleapyear:2012-02-29nonleapyear:2011-02-28
Formatting and time calculations
    • DateTimeFormatter: Converts between a Date object and a string.

    • Chronounit: Calculates the time distance between two points of time, which can be calculated in a variety of time units.

    • Temporaladjuster: various date calculation functions.

Continue with the previous code:

  DayOfWeek DayOfWeek = dayofweek.of (1); System.out.println ("DayOfWeek:" + dayOfWeek);//calculates the number of days between two dates, and calculates the interval between two points of time in other time units. Long between = ChronoUnit.DAYS.between (Localdate, leapyear); System.out.println ("Between:" + between);//thread-safe format class, not each time new Simpledateformatdatetimeformatter DateTimeFormatter = Datetimeformatter.ofpattern ("uuuu MM dd");//Convert date time to string identifier System.out.println ("Date Formatter:" + Datetimeformatter.format (Nonleapyear));//parse the date time as a string temporalaccessor temporalaccessor = Datetimeformatter.parse ( "2013 01 15"); System.out.println ("Temporalaccessor:" + localdate.from (temporalaccessor)); Instant Instant = Instant.now (); Timestamp System.out.println ("Instant:" + instant);//Calculate the date of the first day of the month localdate with = Nonleapyear.with ( Temporaladjuster.firstdayofmonth ()); System.out.println ("with:" + with);//Calculates the date of the first Monday of a month temporaladjuster Temporaladjuster = Temporaladjuster.firstinmonth (Dayofweek.monday); Localdate with1 = Localdate.with (Temporaladjuster); System.out.println ("with1:" + with1);//Calculation LOcaldate the next Monday's date localdate with2 = Localdate.with (Temporaladjuster.next (dayofweek.monday)); System.out.println ("with2:" + with2);

Output:

Dayofweek:mondaybetween: -594date formatter:2011 28temporalaccessor:2013-01-15instant:2013-10-15t07 : 55:30.964zwith:2011-02-01with1:2013-10-07with2:2013-10-21
Java.util.Date conversion to a new library class

Conversions can be done in the following ways.

  Date.toInstant()Date.from(Instant)Calendar.toInstant()
Method overview

The package's API provides a number of related methods, which generally have a consistent method prefix:

    • Of: Static Factory method.
    • Parse: Static Factory method, focusing on parsing.
    • Get: Gets the value of something.
    • is: Check if something is true.
    • With: Immutable setter equivalents.
    • Plus: Add some amount to an object.
    • Minus: Subtracts some amount from an object.
    • To: Convert to another type.
    • At: Combine this object with another object, for example: date.atTime(time) .

Recall that the previous statements are processed in the stored procedure date, because Java's date operation is indeed too weak. With Java 8, mom doesn't have to worry about me working on the date any more.

Related [Java Java.time] Recommendation: Java 8 Java.time Package-code Bee Note Java.time package is introduced in JDK8, providing the main API for date, time, instance and cycle. The classes defined by the Java.time package represent the rules of the date-time concept, including instants, durations, dates, times, time-zones and periods. These are based on the ISO calendar system, which is followed by Gregorian rules. All classes are immutable and thread-safe. LocalDateTime: Stores the date and time, such as:. Localdate: A date is stored, such as:. LocalTime: Stores the time, such as:. The classes above can be combined by the following classes:. Yearmonth: Indicates the month. MonthDay: Represents the day of the month. Lock in Java (Locks in Java)--concurrent Programming Network-ifeve.com original link Jakob Jenkov Translator: Palanisamy proofreading: Ding. Locks, like synchronized synchronous blocks, are a thread synchronization mechanism, but more complex than the synchronized synchronization blocks in Java. Because locks (and other more advanced thread synchronization mechanisms) are implemented by synchronized synchronous blocks, we are not yet completely free of the Synchronized keyword (the translator notes: This is the case before Java 5). Starting with Java 5, the Java.util.concurrent.locks package contains the implementation of some locks, so you don't have to implement your own locks. But you still need to know how to use these locks, and it's useful to understand the theory behind them. You can refer to my introduction to Java.util.concurrent.locks.Lock for more information about locks. Java PaaS Showdown-the melon-IBM developerWorks China: Document library This article compares three main types of data for Java developers Platform as a Service (PaaS) Products: Google App Engine for Java, Amazon Elastic Beanstalk and Cloudbees [email protected] it analyzes The unique technical methods, advantages, and disadvantages of each service, and also discusses common solutions. Learn the basic concepts of foundational Java PaaS and learn how to choose the services that are appropriate for your development. Java floating pointNumber-d0ngd0ng-computer/web/digital technology Thomas Wang, March 2000. The definition of a Java floating-point number generally adheres to the binary floating-point arithmetic standard (the IEEE 754 standard). The IEEE 754 standard provides the definition of floating-point numbers infinity, negative infinity, negative 0, and non-numeric (not a number, referred to as Nan). In Java development, these things are often confused by most programmers. In this article, we will discuss the calculation of these special floating-point related results. We will also point out some common pitfalls of Java floating-point numbers. Two types of built-in categories for floating-point numbers are available in the Java programming language: float and double. Float occupies 4 bytes of storage space, 23 bits are used to represent precision, a double occupies 8 bytes of storage, and 52 bits are used to represent precision. 3.0D is a double-precision floating-point number constant. qt--to Java? --Blog-Bole Online Editor's note: In fact, QT is still one of the best tools for cross-platform development, but QT is not officially supported by any major mobile operating system. Nokia's future plans, which are very vague, are a disappointment to many third-party developers and will cause Nokia's repeated failures. One of QT's main developers, Mirko Boehm, has a strong satirical blog about Nokia's decision to cut the QT division, calling it "a desperate move" rather than a "policy change." Later, RIM extended an olive branch to the cropped QT developers. Since Nokia has stopped Meltemi system development, and now the QT division is disbanded, then the Meego system based on this will also end. Nokia next strategy will not be based on Java. Zhang Yusheng said that Nokia, if based on Java, in fact, just a change of the death method, the next strategy is not good to hear is the sunset strategy for the poor, as a developer from the data, the people who do not spend money to buy a mobile phone, most will not pay for the application. Java Verification Code--Iteye Blog// To create a font, the size of the font should depend on the height of the image. Randomly generates 160 lines of interference so that the authentication code in the image is not easily detected by other programs. The Randomcode is used to save randomly generated verification codes so that users can log in and authenticate. Random generation of Codecount number verification code. Get a randomly generated captcha number. Generates a random color component to construct a color value so that the output will have a different color value for each digit. Draws the verification code into the image with a randomly generated color. The resulting four random numbers are grouped together. Save the four-digit verification code to the session. outputting an image to the servlet output stream.0 people posted a message and punched->> here <<-participate in the discussion. Java-Java-programming language-Iteye blog abstraction is about ignoring aspects of a topic that are not relevant to the current goal in order to pay more attention to the aspects related to the current goal. Abstractions do not intend to understand all of the problems, but simply select one part of them, temporarily without some detail.  Abstract includes two aspects, one is the process abstraction, the other is the data abstraction. Inheritance is a hierarchical model of a junction class and allows and encourages the reuse of classes, which provides a way to articulate commonalities. A new class of objects can be derived from existing classes, a process known as class inheritance. The new class inherits the attributes of the original class, which is called the derived class (subclass) of the original class, and the original class is called the base class of the new class (The parent Class).  Derived classes can inherit methods and instance variables from their base classes, and classes can modify or add new methods to make them more suitable for special needs. Encapsulation is the process and data is surrounded, access to data only through the defined interface. Java uses memcached--Internet-Iteye blog first to http://danga.com/memcached download Memcached's version of Windows and Java client jar packages, Currently the latest version is Memcached-1.2.1-win32.zip and Java_memcached-release_1.6.zip, respectively decompression can be. Then is the installation run Memcached server, we will memcached-1.2.1-win32.zip unzip, enter its directory, and then run the following command: c:>;memcached.exe-d install<br>c: >memcached.exe-l 127.0.0.1-m 32-d start. The first line is to install memcached as a service so that it runs correctly, otherwise the run fails. Java thread pool-Enterprise architecture-the use of Iteye blog threads is of paramount importance in Java, and the use of the thread pool is very rudimentary in the JDK version of jdk1.4. The situation has changed a lot since the jdk1.5. Jdk1.5 later added the Java.util.concurrent package, which focuses on Java threads and the use of thread pools. Provides a great deal of help with the problem of threading in development. The thread pool function is to limit the number of threads executing in the system.      depending on the environment of the system, the number of threads can be set automatically or manually to achieve the mostLess wasted system resources, many of which caused the system congestion efficiency is not high. Use the thread pool to control the number of threads and other threads to wait. A task is completed, and then the first task from the queue is executed. If there is no waiting process in the queue, this resource of the thread pool is waiting. Java Exceptions--CSDN Blog Recommendation article "Good programming language helps programmers write good programs, but no matter what language avoids programmers writing bad programs."                                                                                                                               ----The idea of Java programming.          "giving full play to the advantages of the exception can improve the readability, reliability and maintainability of the program. If used improperly, it can bring a lot of negativeEven if the programming cannot continue.          Anomaly is a double-edged sword, and more often it brings negative effects. In programming, programmers often do not want to deal with abnormal situations or when those anomalies have caused irreversible errors in the program, exception handling may not be able to solve the problem, in addition to the business process to add this exception processing code, inevitably affect the meaning of the class itself, Reduced readability and maintainability of the program.

Java8 java.time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.