Calendardemo Calendar class creation and usage

Source: Internet
Author: User

The/**calendar  class, which mainly acts on its methods, can operate on the time component. It provides some methods for converting between a particular instant and a set of calendar fields such as  year, MONTH, Day_of_month, hour , and so on. and provides some methods for manipulating calendar fields. It is an abstract class that provides a factory method: Calendar getinstance (). This method can be implemented according to the region of the current system to obtain an appropriate child class of the calendar.   Main methods: 1.void set (Int field,int value) The method can set different values for different time components respectively. The calendar provides the corresponding constants for different time components, and when we use the Set method setting, the first parameter should use the corresponding constant as the time component.  2.int get (Int field) The method can obtain the corresponding value by the different time components. The calendar provides the corresponding constants for different time components, and when obtained using the Get method, the parameters should use the corresponding constants as the time component.  3.void add (Int field,int amount) The method can add a given value to the value of a time component, and if you want to subtract a given value, the incoming value needs to be a negative number.  4. SetTime and GetTime method   (calendar ---->   date) Calendar void settime (date date ), allowing us to set the time represented by the date object for the calendar.   (date  ---->   calendar) Calendar of Date gettime (), Allows us to get a time represented by the calendar described using the Date object.  5.int getactualmaximum (Int field) This method is used to obtain the maximum allowable value for a given time component, for example: Gets the last day of the month in the current system time (maximum value allowed by day) Int max  = calendar. getactualmaximum (Calendar.day_Of_month);   This case requires getting an instance of the Calendar class, getting an instance after doing the following: 1)   Gets the actual type that the calendar instance belongs to and outputs. 2)   Converts the Calendar object to a Date object and outputs the Date object. 3)   Use GregorianCalendar to build an object that corresponds to a date of December 25, 2013, converts the GregorianCalendar object to a Date object, and outputs the date object.       about the Date,simpledateformat,calendar of the three-person process           *  general processes and scenarios for use with.          *  the user enters a date and then, based on a series of calculations, finally shows the date to the user           *  for example;          *   The user enters a product's production date, and the days of the shelf life, to view the expiration date.          *  Process:          * 1. User enters a date, and other information          * 2. Converts the user input date (string) through          * simpledateformat date          * 3. Converting date to CALENDAR &NBSp;       * 4. A series of calculations for time through the calendar           * 5. Convert the calculated calendar representation time back to date          * 6. Converts a date through SimpleDateFormat to a string          * 7. Output to User date results.  */
 Public classCalendarDemo1 { Public Static voidMain (string[] args) {/*This case requires that you obtain an instance of the Calendar class, and then do the following to get the instance:*/Calendar Calendar=calendar.getinstance ();        System.out.println (Calendar.getclass (). GetName ()); /*output: Java.util.GregorianCalendar * Calendar is an abstract class, its specific subclasses for different countries of the calendar system, * The most widely used is GregorianCalendar (its calendar         , the Universal solar calendar), which corresponds to the standard calendaring system used in most countries around the world. * */        //2) Convert the Calendar object to a Date object and output the Date object. System.out.println (Calendar.gettime ()); //Sat Apr 17:57:43 gmt+08:00//3) Use GregorianCalendar to build the object that corresponds to the date of December 25, 2013,//converts the GregorianCalendar object to a Date object and outputs the Date object.         /*Calendar Construction Method protected Calendar (TimeZone Zone,locale Alocale)*/GregorianCalendar C1=NewGregorianCalendar (2013,gregoriancalendar.december,25);            System.out.println (C1.gettime ()); //Wed Dec 00:00:00 gmt+08:00    }}

Calendardemo Calendar class creation and usage

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.