Time Processing related classes
"Time, like water, gone forever," time is a one-dimensional east. So, we need a scale to express and measure time. In the computer world, we set the January 1, 1970 00:00:00 as the base time, each unit of measure being milliseconds (1 seconds of 1 per thousand).
We use a long variable to represent time, hundreds of millions of years ahead of the base time, and hundreds of millions of years from the next. If you want to get the "moment value" of the present moment, you can use:
long Now1 = System. Currenttimemillis ();
This "moment value" is the core value of all time classes, and the month and day are calculated based on this "value". The time-related classes involved in our work learning have the following:
Java.util.Calendar Calendar Time calculation with him
Java.util.Date Core Class
Java.text.DateFormat time Formatting
Figure 1st period Time related class
Core class (Java.util.Date)
Date Time Class (Java.util.Date)
Contains a date class in the standard Java class library. Its object represents a specific moment, accurate to milliseconds.
Date () assigns a Date object and initializes the object to the current date and time to milliseconds.
(long date) allocates a Date object and initializes the object to represent the specified number of milliseconds since the standard base time, known as the epoch, or January 1, 1970 00:00:00 GMT.
A Boolean after (date time) tests whether this date is after the specified date.
Booleanbefore (date when) tests whether this date is before the specified date.
A Boolean equals (Object obj) compares the equality of two dates.
Long GetTime () returns the number of milliseconds represented by this Date object since January 1, 1970 00:00:00 GMT.
String toString () converts this Date object to a string of the following form:
Dow Mon dd hh:mm:ss zzz yyyy where: Dow is one day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
The example 1 " Date use of the class
1 import java.util.Date;2 3 Public classTest {4 5 Public Static voidMain (string[] args) {6 7Date date1 =NewDate ();8 9System. out. println (Date1.tostring ());Ten One Longi =date1.gettime (); A -Date Date2 =NewDate (I- +); - theDate Date3 =NewDate (i + +); - -System. out. println (Date1.after (Date2)); - +System. out. println (Date1.before (Date2)); - +System. out. println (Date1.equals (Date2)); A atSystem. out. println (Date1.after (Date3)); - -System. out. println (Date1.before (Date3)); - -System. out. println (Date1.equals (Date3)); - inSystem. out. println ( - to NewDate (1000L* -* -* -*365*39L). ToString ());//Why ? + - } the *}
Viewing the API documentation you can see that many methods are obsolete, and the date before JDK1.1 contains the following: dates, strings converted to time objects, time objects.
After JDK1.1, the date operation uses: Canlendar class. String conversions: DateFormat.
Time formatting
DateFormat class and SimpleDateFormat class
• function
Converts a time object into a string of the specified format. Conversely, converts a string of the specified format into a time object.
DateFormat is an abstract class that generally uses the SimpleDateFormat class.
The example 2 " DateFormat Classes and SimpleDateFormat use of the class
1 import java.text.ParseException;2 3 import Java.text.SimpleDateFormat;4 5 import java.util.Date;6 7 Public classTest {8 9 Public Static voidMain (string[] args) throws ParseException {Ten One //new out SimpleDateFormat object A -SimpleDateFormat S1 =NewSimpleDateFormat ("YYYY-MM-DD Hh:mm:ss"); - theSimpleDateFormat s2 =NewSimpleDateFormat ("YYYY-MM-DD"); - - - + //converting a Time object into a string - +String daytime = S1.format (NewDate ()); A atSystem. out. println (S2.format (NewDate ())); - -System. out. println (NewSimpleDateFormat ("Hh:mm:ss"). Format (NewDate ())); - - - in //converts a string that conforms to the specified format into a time object. The string format needs to be consistent with the specified format. - toString time ="2007-10-7"; + -Date Date =S2.parse (time); the *System. out. println ("date1:"+date); $ Panax NotoginsengTime ="2007-10-7 20:15:30"; - theDate =S1.parse (time); + ASystem. out. println ("Date2:"+date); the + } - $}
The meaning of a table formatted character
Letters
Date or time element
Said
Example
G
Era Marker
Text
AD
y
Years
Year
1996;96
M
Month of the Year
Month
July; Jul;07
w
年中的周数
Number
27
W
Number of weeks in the month
Number
2
D
年中的天数
Number
189
d
Number of days in month
Number
10
F
Week of the Month
Number
2
E
Days of the Week
Text
Tuesday;Tue
a
AM/PM Mark
Text
PM
H
Number of hours in the day (0-23)
Number
0
k
Number of hours in the day (1-24)
Number
24
K
Number of hours in am/pm (0-11)
Number
0
h
Number of hours in am/pm (1-12)
Number
12
m
Number of minutes in hours
Number
30
s
Number of seconds in minutes
Number
55
S
Number of milliseconds
Number
978
z
Time
General Time Zone
Pacific Standard Time; PST;GMT-08:00
Z
Time
RFC 822 Time Zone
0800
Common class one by one time processing related classes one by one java.util.Tomezone (Java.util.Calendar, Java.util.Date, Java.text.DateFormat)