Time operation in Java

xiaoxiao2021-03-31  213

Time operation in Java

Text / ray

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:

LetterDate or Time Component Presentation GEra designator Text yYear Year M Month in year Month w Week in year Number W Week in month Number D Day in yearNumber d Day in month Number F Day of week in month Number E Day in week Texta Am / pm marker TEXT H HOUR in Day (0-23) Number K Hour in Day (1-24) Number K Hour in AM / PM (0-11) Number H Hour in AM / PM (1-12) Number M Minute In Hour Number S Second IN minute Number S Millisecond Number Z Time Zone General Time Zone Z Time Zone RFC 822 Time Zone Related Articles: Time Operation in Java, Part 2

转载请注明原文地址:https://www.9cbs.com/read-130838.html

New Post(0)