Getting started with JavaSE 32: time and date related classes of common Java classes
In Java programs, Date-related classes include java. util. Date, java. text. SimpleDateFormat, java. util. calendar, and
Java. util. GregorianCalendar class. Here we will briefly introduce the use of the Date, SimpleDateFormat, and calendar classes.
A Date class indicates that time (1) recognizes the Date class
In program development, we often need to process Date and time-related data. In this case, we can use the Date class in the java. util package. This class is the most important
Is used to obtain the current time. The java. util package provides the Date class to encapsulate the current Date and time.
The Date class provides two constructors to instantiate the Date object:
1) The first constructor uses the current date and time to initialize the object:
Date( )
2) The second constructor receives a parameter, which is the number of microseconds from January 1, January 1, 1970:
Date(long millisec)
After the Date object is created, you can call the following method:
Let's take a look at the use of the Date class. It is very easy to get the current Date and time in Java. Use the toString () method of the Date object to print the current Date and
Time, as shown below:
The instance code is:
Import java. util. *; public class Test {public static void main (String [] args) {// initialize the Date object Date d = new Date (); // directly output the Date time System. out. println (d); // use the toString () function to display the date and time System. out. println (d. toString ());}}
The object created by using the default no-argument constructor of the Date class represents the current time. We can directly output the Date object to display the current time
You can use the toString () method of the Date object to print the result of the current Date and time display as follows:
Among them, Mon represents Monday, Mar represents March (March), 28 represents No. 28, CST represents China Standard Time (China Standard Time
Quasi-time, that is, Beijing time, UTC + 8). 2016 represents January 1, 2016.
(2) date comparison
Java uses the following three methods to compare two dates:
1) Use the getTime () method to obtain two dates (the subtle values experienced since January 1, January 1, 1970) and then compare these two values.
2) use before (), after (), and equals (). For example, if the 12 th day of a month is earlier than the 18 th day, newDate (99, 2, 12). before (new Date
(99, 2, 18) returns true.
3) The compareTo () method is defined by the Comparable interface, which is implemented by the Date class.
Ii. Use SimpleDateFormat to format a date
From the above output results, we found that the default time format is not very friendly and is not the same as the date format we see on a daily basis. If you want
Display in the specified format, for example, 09:22:30. What should I do?
SimpleDateFormat is a class that formats and analyzes dates in a language-sensitive manner. SimpleDateFormat class allows you to select any
Which of the following statements can be run in custom Date and Time formats. In this case, the SimpleDateFormat class in the java. text package can be used.
To format the date and time. For example, you can convert a date to a text in the specified format or convert the text to a date.
(1) convert a date to a text in the specified format using the format () method
The instance code is:
Import java. util. *; import java. text. *; public class Test {public static void main (String [] args) throws ParseException {// create Date d = new Date (); // create SimpleDateFormat object, specify the target format SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // call the format () method, format time, string day = sdf. format (d); // output the converted string System. out. println ("current time:" + day );}}
In the Code, "yyyy-MM-dd HH: mm: ss" is a predefined formatted string. yyyy indicates four-digit year, MM indicates two months, and dd indicates two days.
Period, HH indicates the hour (in the 24-hour format), mm indicates the minute, and ss indicates the second. In this way, the target format of the conversion is specified, and the format () method is called.
Converts time to a string in the specified format.
Running result:
(2) convert text to date using the parse () method
Instance code:
Import java. util. *; import java. text. *; public class Test {public static void main (String [] args) throws ParseException {// create Date Format String day = "March 01, 2016 10:30:25"; // create SimpleDateFormat object, specify the target format SimpleDateFormat sdf = new SimpleDateFormat ("yyyy MM dd HH: mm: ss"); // call the parse () method, convert string to Date date Date = sdf. parse (day); // output the converted string System. out. println (date );}}
In the Code, "yyyy MM dd HH: mm: ss" specifies the date format of the string, and calls the parse () method to convert the text to the date.
Running result:
(3) Precautions
1) when you call the parse () method of the SimpleDateFormat object, a conversion exception (ParseException) may occur.
Therefore, this method is not frequently used.
2) When using the Date class, you must import the java. util package. When using SimpleDateFormat, you must import the java. text package.
Instance code:
Import java. util. *; import java. text. *; public class Test {public static void main (String [] args) throws ParseException {// use format () method: Convert the date to the text SimpleDateFormat sdf1 = new SimpleDateFormat ("MM dd, yyyy, HH, mm, ss seconds") in the specified format "); simpleDateFormat sdf2 = new SimpleDateFormat ("yyyy/MM/dd HH: mm"); SimpleDateFormat sdf3 = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); // create a Date object, which indicates the current time Date now = new Date (); // call the format () method, convert the Date to a string, and output the System. out. println (sdf1.format (now); System. out. println (sdf2.format (now); System. out. println (sdf3.format (now); // use parse (method to convert text to date String d = "21:05:36 "; simpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // call the parse () method to convert the string to Date date Date = sdf. parse (d); System. out. println (date );}}
Running result:
3. Applications of the Calendar class (1) Recognition of the Calendar class
The main function of the Date class is to obtain the current time. At the same time, this class also has the time setting and some other functions.
But these methods have been criticized and are not recommended. We recommend that you use the Calendar class to process the time and date.
The java. util. Calendar class is an abstract class. You can call the getInstance () static method to obtain a Calendar Object.
Initialize the date and time before, that is, the default value is the current time. Create a Calendar Object that represents the current date of the system, for example:
Calendar c = Calendar. getInstance (); // The default value is the current date.
(2) Field Types of Calendar objects
The Calendar class uses these constants to express different meanings. Many classes in jdk actually use this idea:
So how can I use Calendar to obtain information such as year, month, day, and time? Let's look at the following code:
Import java. util. *; public class Test {public static void main (String [] args) {// create the Calendar Object Calendar c = Calendar ar. getInstance (); // get the year int year = c. get (Calendar. YEAR); // obtain the month, 0 indicates the month of the month int month = c. get (Calendar. MONTH); // obtain the date int day = c. get (Calendar. DAY_OF_MONTH); // get the hour int hour = c. get (Calendar. HOUR_OF_DAY); // get the minute int minute = c. get (Calendar. MINUTE); // get the second int second = c. get (Calendar. SECOND); // output result System. out. println ("current time:" + year + "-" + (month + 1) + "-" + day + "" + hour + ":" + minute + ": "+ second );}}
Call the getInstance () method of the Calendar class to obtain an instance, and then call the get () method to obtain the date and time information. The parameter is required.
The value of the field to be obtained, such as Calendar. Year, is a static constant defined in the Calendar class.
Running result:
(3) Common Calendar Methods
The Calendar class provides the getTime () method to obtain the Date object, convert the Calendar and Date, and use getTimeInMillis ()
Method to obtain the time value of this Calendar, in milliseconds. As follows:
Import java. util. *; public class Test {public static void main (String [] args) {// create the Calendar Object Calendar c = Calendar ar. getInstance (); // convert the Calendar Object to the Date object Date date = c. getTime (); // get the current number of seconds Long time = c. getTimeInMillis (); // output result System. out. println ("current time:" + date); System. out. println ("Current millisecond count:" + time );}}
Running result:
Instance code:
Import java. util. *; import java. text. *; public class Test {public static void main (String [] args) {// create the Canlendar object Calendar c = Calendar. getInstance (); // convert the Calendar Object to the Date object Date date = c. getTime (); // create the SimpleDateFormat object and specify the target format SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); // convert the date to a String in the specified format String now = sdf. format (date); System. out. println ("current time:" + now );}}
Running result: