The date and SimpleDateFormat classes represent time
In program development, it is often necessary to deal with date and time related data, when we can use the date class in the Java.util package. The main function of this class is to get the current time, let's look at the use of the Date class:
The object created by using the default parameterless construction method of the date class represents the current time , and we can directly output a Date object that displays the current time, showing the following results:
Among them, Wed represents Wednesday (Wednesday), June stands for June (June), 11 for 11th, CST for China Standard Time (Beijing time, the East eight district).
From the above output, we found that the default time format is not very friendly, and we see the date format is not the same, if you want to display in the specified format, such as 2014-06-11 09:22:30, then how to do?
At this time to the Java.text package of SimpleDateFormat class to do their time!! You can use SimpleDateFormat to format datetime, such as to convert a date to text in a specified format, or to convert text to a date.
1. Use the format () method to convert a date to a text in the specified format
The code "YYYY-MM-DD HH:mm:ss" is a predefined string, YYYY represents four-bit years, MM for two-bit months, DD for two-bit dates, HH for hours (using 24-hour system), MM for minutes, SS for seconds, so that the target format of the transformation is specified, and the last Call The format () method converts the time to a string of the specified format.
Running result: 2014-06-11 09:55:48
2. Use the parse () method to convert text to a date
The "yyyy mm DD day HH:mm:ss" in the code specifies the date format of the string, and the parse () method is called to convert the text to a date.
Operation Result:
Must pay attention to OH:
1. A conversion exception may occur when invoking the parse () method of the SimpleDateFormat object, or parseexception, so exception handling is required
2. You need to import the Java.util package when using the Date class, you need to import the Java.text package when using SimpleDateFormat
Second, Calendar class
The most important role of the date class is to get the current time, while the class also has set time and some other functions, but because of its own design problems, these methods have been criticized many, not recommended, it is recommended to use the Calendar class for time and date processing.
The Java.util.Calendar class is an abstract class that can get a Calendar object by calling the getinstance () static method, which is initialized by the current datetime, which means the current time by default, such as Calendar C = Calendar.getinstance ();
So how do you use Calendar to get information about the year, month, day, time, and more? Let's look at the following code:
Where you call the GetInstance () method of the Calendar class to get an instance, and then get the datetime information by calling the Get () method, the parameter is the value of the field that needs to be obtained, calendar.year, and so on as the static constant defined in the Calendar class.
Operation Result:
The Calendar class provides the GetTime () method for getting the date object, completing the conversion of the calendar and date, and the Gettimeinmillis () method to get the time value of this calendar, in milliseconds. As shown below:
Operation Result:
Third, using the Math class to manipulate data
The math class is located in the Java.lang package and contains methods for performing basic mathematical operations, all methods of the math class are static methods, so you can use the class name directly when using the methods in that class. Method name, such as: Math.Round ();
Common methods:
Through the case we come to know their use it!!
Operation Result:
The Ps:math class also offers a number of other ways that you can focus on wikis and find out more about them.
Use the date and SimpleDateFormat classes to represent the time, Calendar class, and math class