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