Time operation in Java
Time operation in Java is nothing more than these four situations:
1, get the current time
2, get some format for a certain time
3, set time
4, time operation
Ok, the following is aimed at these three situations.
First, get the current time
There are two ways to get, the first, use the DATE class.
There are two DATE classes in J2SE, one is java.sql.date, one is java.util.date
Here, use java.util.date. Get the code of the current time as follows
Date Date = new date (); Date.gettime ();
There is another way to use system.currenttimemillis ();
The results obtained in these two ways are the same, which is a millisecond value of a long time of the current time. This value is actually the current time value and zero zero seconds from January 1 from 1970. Mixtale number.
The current time is got, but the actual application is often not to use this long type, and the user wants to get a string, such as "June 18, 2006", or "2006-06- 18 ", foreigners may wish to get" 06-18-2006 ", and so on. This is the next question to be solved.
Second, get some format for a certain time
Get time format, you need to use a class java.text.SIMPLEDATEFORMAT specifically for time format.
First, define a SimpleDateFormat variable
SimpleDateFormat SDF = New SimpleDateFormat (", Locale.SIMPLified_CHINESE);
The definition of this constructor is as follows:
SimpleDateFormat (String Pattern, Locale Locale)
The first parameter pattern, we will explain it later, here we use "", the second parameter is used to set the time zone, here is used here, this class has defined a lot of static variables. I will use it directly to use OK. We set the time zone to local.simplified_CHINESE, just look at the name, this static variable is very clear.
Next, we use this SimpleDateFormat to format the current time as a time string "XXXX XX XX] when XXXX XX in XXX, the code:
SDF.ApplyPattern ("YYYY MM MM DD Day _HH MM] SS Second"); string timeStr = SDF.FORMAT (New Date ());
The function of the time format is Format, the parameter of this function is a java.util.date object, this is not a flower head.
To illustrate this pattern, so-called mode. Here, YYYY, MM, DD, etc., this is mode.
We can specify a mode in the SIMPLEDATEFORMAT constructor, such as
SimpleDateFormat SDF = New SimpleDateFormat ("YYYY-MM-DD", locale.simplified_chine);
You can also use the ApplyPattern function to temporarily specify when you get the time format. The above example is the case.
What character represents what, this is the J2SE is approving, and we can use the agreement of the string we want to use.
The agreed list of the model represented by J2SE is as follows: