Conversion of Java date types

Source: Internet
Author: User
1. in Java, date and calendar are mainly used to process time and date. because the project involves the processing of the timestamp postmark type obtained from the database. date is not recommended for date processing. calendar is the inheritance and extension of date. calendar is an abstract class, so the general new method is not usable. java provides the implementation subclass --- gregoriancalendar. by default, the calendar instance is obtained through the getinstance () method of the calendar ar type. in general, calendar is more like a large Date Processing Factory. it provides various methods and static constants. each part of the date can be disassembled into small parts and processed for each part. then it is assembled and converted to the corresponding date type. 2. provided by calendar. set (INT fields, INT) method. used to set the corresponding time. 3.add() is used to modify the corresponding date value. 4. in addition to automatically obtaining system default values You can also accept user-defined data. mainly date and long parameters. calendar has two methods: settime (date) and settimeinmillis (long ). calendar. settime (date) can be used to convert a date object to a Calendar Object. the gettime () method returns a date object. it can be seen that the conversion between the date and the calendar type is relatively easy. the add () method in the calendar class has powerful functions. add () can be used to calculate the calendar field. If you need to subtract a value, you can use a negative value, for example, add (field,-value ). Add () has two rules: When the modified field exceeds its permitted range, the field larger than it will be automatically corrected. For example, calendar cal1 = calendar. getinstance (); cal1.set (2000, 7, 31, 0, 0, 0); // 2000-8-31cal1.add (calendar. month, 1); // 2000-9-31 => 2000-10-1, right? System. out. println (cal1.gettime (); // The result is 2000-9-30. Another rule is that if a field smaller than it is immutable (determined by the calendar implementation class ), this small field is changed to the minimum value. In the preceding example, 9-31 is changed to 9-30, because the change is minimal. Conversion of the calendar and timestamp types. there is no direct way to convert the response. calendar <---> long <---> timestamp calendar. settimeinmillis (long) and calendar. gettimeinmillis () returns long timestamp. settime (long) and timestamp. gettime () returns long, which enables the conversion between the two.

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.