Java basics: 16th about date and time usage

Source: Internet
Author: User

 

In Java applications, date and time are widely used as basic information types, such as obtaining the current time and calculating the execution time of some operations. Date Processing-related classes include date and calendar. You can use date and calendar to obtain the current time and perform some operations on the time, obtain the year, month, day, hour, minute, second, Week, and other information in the time. Time in the string format is often used in input and output, which requires mutual conversion between time and string. The system provides dateformat and simpledateformat to complete this conversion. Java. util. Date classUsed to indicate the date and time. To obtain the current time, use the following code: [Example] // simpledatetest. java import Java. util. date; public class simpledatetest {public static void main (string [] ARGs) {// defines the time object date d = new date (); // outputs the time system according to the default format. out. println (D. tostring () ;}}the following is the output result: Mon Dec 11 05:32:13 GMT 2006. To output data in a specific format, follow the following method, however, we recommend that you do not use the current version. [Example] // simpledate2test. java import Java. util. date; public class simpledate2test {public static void main (string [] ARGs) {// defines the time object date d = new date (); // gets the year int year = D. getyear () + 1900; // obtain the monthly int month = D. getmonth () + 1; // get the day int date = D. getdate (); // int hour = D. gethours (); // get the minute int minute = D. getminutes (); // get the second int second = D. getseconds (); // construct the output string system. out. println (Year + "-" + MON Th + "-" + date + "" + hour + ":" + minute + ":" + second) ;}} the execution result is as follows: 5:35:47 if you want to determine a date object based on the year, month, and day, you can create an object and use the setter method to complete it, such as setyear (INT) and setmonth (INT, of course, these methods are not recommended. If you want to flexibly process the time, you can use dateformat and simpledateformat. To output data in the format of "5:35:47", use the following code: [Example] // formatdatetest. java import Java. util. date; import Java. text. dateformat; import Java. text. simpledateformat; public class formatdatetest {public static void main (string [] ARGs) {// creation time object date d = new date (); // create a time format object dateformat df = new simpledateformat ("mm DD, yyyy, HH: mm: SS seconds"); // format the time string STR = DF. format (d); // output the formatted time system. out. printl In N (STR) ;}} format, yyyy indicates the year. You can write two digits, MM indicates the month, 1 digit, DD indicates the day, 1 digit, and HH indicates the hour, MM indicates minute (case sensitive) and SS indicates second. [Note] dateformat and simpledateformat must be introduced in the Java. Text package. To convert a date string to a time string, for example, convert "2006-2-6" to a date, use the following code: [Example] // parsedatetest. java import Java. util. date; import Java. text. dateformat; import Java. text. simpledateformat; public class parsedatetest {public static void main (string [] ARGs) {// defines the date string dates = "2006-2-6 "; // define the date string format dateformat df2 = new simpledateformat ("yyyy-mm-dd"); // declare the date object date D2; try {// convert the date string to date D2 = df2.parse (dates); system. out. Println (df2.format (D2);} catch (exception ex) {}} [note] exception handling is required during conversion, because exceptions may occur during conversion. Java. util. Calendar classCalendar provides many methods to operate on the time in the middle, month, day, hour, minute, second, and week. If you want to perform more detailed operations on the time, you can use calendar ar. This class is also an abstract class. When using this class, you need to use getinstance to obtain the instance and then perform operations. This method can obtain the instance corresponding to the specific time zone. If no parameter is specified, the default time is obtained. The following code gets the current time: Calendar C1 = calendar. getinstance (); to obtain the specific year, month, day, hour, minute, and second or other information in the time, use the get method to specify the information to be obtained, for example, you can use the following code to obtain the year, month, and day: year = c1.get (calendar. year); month = c1.get (calendar ar. month) + 1; Date = c1.get (calendar. date); to modify a certain item in the time, use the set method. The method is defined as follows: public void set (INT field, int value). The first parameter specifies the modified item, the second parameter indicates the modified value, for example, changing the year to January 1, 2003: c1.set (calendar. year, 2003); if you want to modify the year, month, and day at the same time, you can use the following method: Public void set (INT year, int month The, int date) parameter represents the hour, minute, and second respectively. The following example modifies the year, month, and day at the same time: c1.set (, 5 ); the following example shows how to modify the values of year, month, day, hour, minute, and second at the same time. // modify the c1.set (, 30); // modify the value of c1.set ); date objects and calendar objects can be converted to each other. The following example shows how to convert a calendar object to a date object D2 = c1.gettime (); // convert the date type to the calendar type c1.settime (D2). For usage of other methods, refer to the JDK help documentation. Last Lecture: Lecture 2 Usage Next Lecture: 17th Basic Input and OutputLi xucheng csdn blog: http://blog.csdn.net/javaeeteacher invites you as a friend: http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.