Java 8:date-time Apis__java

Source: Internet
Author: User

This article is based on Oracle Java official documentation, after personal practice to organize and accumulate, for reference only. 1 Overview

JDK 8 introduces a new date-time APIs, consisting of the main package java.time and 4 child packages: Java.time java.time.chrono java.time.format java.time.temporal java.time.zone

Method naming Specification

prefix Method Type Use
Of Static Factory Creates an instance where the factory are primarily validating the input parameters, not converting them
From Static Factory Converts the input parameters to a instance of the target class, which may involve losing information the input
Parse Static Factory Parses the input string to produce an instance of the target class.
2 Course

There are 2 kinds of time rendering methods in Java:
(1) The time of the human world, such as year, month, day, hour, minute, second
(2) The time of the Machine world, a time axis for the era, the statistical distance of this era has disappeared Nanosecond (One-zero seconds) value
JDK 8 introduces the new date time APIs that cover both time rendering, so you need to determine how the time is presented before you select the specific API, and refer to the following table

class or enumeration years Month Day hours minutes seconds area deviations Zone ID string Output
Instant Y


2.1 DayOfWeek
(1) Java.time.DayOfWeek is an enumeration type, consisting of 7 constants representing seven days a week

Sample code
package test;

Import Java.time.DayOfWeek;

Import Org.junit.Test;

public class Datetimetest {

    @Test public
    void Test () {
        System.out.println ("Dayofweek.monday:" + Dayofweek.monday);
        System.out.println ("Dayofweek.tuesday:" + dayofweek.tuesday);
        System.out.println ("Dayofweek.wednesday:" + dayofweek.wednesday);
        System.out.println ("Dayofweek.thursday:" + dayofweek.thursday);
        System.out.println ("Dayofweek.friday:" + dayofweek.friday);
        System.out.println ("Dayofweek.saturday:" + dayofweek.saturday);
        System.out.println ("DayOfWeek.Sunday:" + dayofweek.sunday);
    }

Test results
DayOfWeek.MONDAY:MONDAY
DayOfWeek.TUESDAY:TUESDAY
DayOfWeek.WEDNESDAY:WEDNESDAY
DayOfWeek.THURSDAY:THURSDAY
DayOfWeek.FRIDAY:FRIDAY
DayOfWeek.SATURDAY:SATURDAY
DayOfWeek.SUNDAY:SUNDAY

(2) integer value range [1,7] can be calculated by adding and decreasing numbers
(3) using the GetDisplayName (TextStyle, Locale) method to customize the string content

//sample code 
Related Article

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.