1. Millisecond value concept and function of
/*
Java. Util. The Date class: used to describe the Date and time of class
Class Date said specific moment, accurate to milliseconds,
1 seconds=1000 milliseconds
1 day=24 * 60 * 60=86400 * 1000=86400000 milliseconds
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The origin time: 0 millisecond time
On January 1, 1970 00:00:00 GMT British standard time (world)
East China: eight time + 8 hours of time
On January 1, 1970 08:00:00
Can put the date and time into a millisecond value: is a difference in the time origin and current time
Such as time (11:36:28 02 in 04, 2020) - the time origin (00:00:00) on January 1, 1970=the date and time conversion to millisecond value (1574132468346)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Millisecond value function: to calculate time
Work: calculate the difference between the two time, put the time into a millisecond value calculation
"On November 19, 2019 11:00:28" - "on October 19, 2018 11:00:28"=can't calculate
1574132468346-15741222468346=234234111 milliseconds value==& gt; Is converted to a time value/1000/60/60/24 (234234111 milliseconds) day
*/
Public class Demo01Date {
Public static void mian (String [] args) {
System. The out. Println (System. CurrentTimeMills ());
}
}
2. The Date class constructor and member method
/*
Java. Util. The Date class
Method:
Date () key
The Date (the Date)
Member methods:
Long getTime () key
Return since 00:00:00 GMT on January 1, 1970 the number of milliseconds the representation of a Date object,
Void setTime (long time)
Set the Date object to said 00:00:00 GMT time after January 1, 1970 milliseconds point in time,
*/
Public class Demo01Date {
Public static void main (String [] args) {
//show01 ();
//show02 ();
//show03 ();
Show04 ();
}
/*
Members of the Date class method
Void setTime (long time) microseconds values, convert millisecond value to Date Date
This method is equivalent to the Date class constructor with parameters
Date (Date) microseconds values, convert millisecond value to Date Date
*/
Private static void show04 () {
Date date=new Date();
The date. The setTime (0, l);
System. The out. Println (date);//Thu Jan 01 08:00:00 CST 1970
}
/*
Members of the Date class method
Long getTime () put the date into a millisecond value (the current time and the time between the origin experienced how many milliseconds)
*/
Private static void show03 () {
Date date=new Date();
long time=date.getTime();
System. The out. Println (time);//1619364584395
System. The out. Println (System. CurrentTimeMillis ());//1619364661226
}
/*
The Date class constructor with parameters:
` Date (Date) microseconds values, convert millisecond value to Date Date
Note:
In the time and date related classes, long type of value is ms
*/
Private static void show02 () {
The Date d1=new Date (0, l);
System. The out. Println (d1);//Sun Apr 25 23:36:24 CST 2021
D1=new Date (12345 l);
System. The out. Println (d1);//Thu Jan 01 08:00:12 CST 1970
}
/*
The Date class empty constructor parameters:
Date () to obtain the current system Date and time of the
(Beijing | Shanghai CST: China standard time)
*/
Private static void show01 () {
Date date=new Date();
System. The out. Println (date);//Sun Apr 25 23:36:24 CST 2021
}
}
5. The DateFormat
Java. Text. The DateFormat class extends the Format
DateFormat is date/time formatting a subclass of the abstract class==& gt; Format is a subclass of a class, or an abstract class (unable to create object)
Function:
Format (i.e. date - & gt; Text)
Parsing (text - & gt; Date)
Member methods:
String format (the Date Date) according to the specified mode, put the Date format to conform to the pattern String
The Date the parse (String source) to conform to the pattern of String parsing for Date
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
DateFormat, is an abstract class cannot directly create objects used, need to use the DateFormat subclass
Java. Text. SimpleDateFormat extends the DateFormat
Method:
SimpleDateFormat (String pattern)
With the given structure mode and the default language environment date format symbols SimpleDateFormat,
Parameters:
String pattern: delivery date and time of pattern
In the pattern of write y represent years
In the pattern of write
M on behalf of the monthIn the pattern to write d represent day
In the pattern of writing H representing
In the pattern of write m representative points
Written in the model s is second
"MM - dd yyyy - HH: MM: ss"
"Yyyy/MM/dd HH/ss"
"On dd MM yyyy years HH MM ss second"
Note:
Said model letters can't change (y, M... ), letter of connection symbol can write
Public class Demo01DateFormat {
Public static void main (String [] args)/* throws a ParseException */{
Show02 ();
}
/*
The Date the parse (String source) to conform to the pattern of String parsing for Date
Implementation steps:
1. Create a SimpleDateFormat object, the constructor to specify model
2. Call SimpleDateFormat object methods in the parse, to conform to the pattern of string parsing for date
*/
Private static void show02 ()/* throws a ParseException */{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
/*
Public Date parse (String source) throws a ParseException
Parse method throws parsing exceptions ParseException
When passing strings and pattern don't match, this exception will be thrown
Calling a method throws an exception, there are two types of treatment: throws continue to throw or try... Catch processing
Alt + enter
*/
The Date the Date=null;
Try {
The date=SDF. Parse (" 2000/01-01 ");
} the catch (ParseException e) {
e.printStackTrace();
}
System. The out. Println (date);
}
/*
String format (the Date Date) according to the specified mode, put the Date format to conform to the pattern String
Implementation steps:
1. Create a SimpleDateFormat object, the constructor to specify model
2. Call methods on the SimpleDateFormat object format, according to the incoming mode in the constructor, the date format to conform to the pattern string
*/
Private static void show01 () {
//SimpleDateFormat SDF=new SimpleDateFormat (" MM - dd yyyy - HH: MM: ss ");
//SimpleDateFormat SDF=new SimpleDateFormat (" on dd MM yyyy years HH MM cent ss ");
SimpleDateFormat SDF=new SimpleDateFormat (" yy years on d M H M points s seconds ");//21, 04269, 00 39 points 02 seconds when
Date date=new Date();//Mon Apr 26 00:39:14 CST 2021
System. The out. Println (date);
String s=SDF. The format (date);
System.out.println(s);//the 2021-04-26 00:39:14
}
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull