Calendar class for javaapi Learning

Source: Internet
Author: User

The following are common fields and methods in the calendar class:

Package util; import Java. text. simpledateformat; import Java. util. calendar; import Java. util. date; /***** @ author yjmao * @ deprecated calendar common methods and common attribute summary * @ version v1.0.0 */public class learncalendar {public static void main (string [] ARGs) {// common method param (); // common attribute method () ;}// common method public static void method () {date = new date (); calendar c = calendar. getinstance (); // settime (): Use the given date to set this Cal Endar time C. settime (date); // obtain the Calendar Object calendar CM = calendar. getinstance (); // gettime (): Get the current time, similar to new date (); Date d = cm. gettime (); system. err. println ("calendar acquisition time:" + d); system. err. println ("new date creation time:" + date); // gettimeinmillis (): returns the time value of this calendar, in milliseconds. Long DL = C. gettimeinmillis (); long ddate = cm. gettimeinmillis (); system. err. println ("milliseconds:" + dl); system. err. println ("millisecond count:" + ddate); // settimeinmillis (): set the current time value of the calendar with the given long value. Long SV = 123456; Calendar SC = calendar. getinstance (); SC. settimeinmillis (SV); simpledateformat Ss = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); string ST = ss. format (SC. gettime (); system. err. println (ST); // get (): returns the value of the given calendar field. Int year = C. get (calendar. year); system. err. println (year); // set (): sets the specified calendar field to the specified value c. set (calendar. year, 2); int y = C. get (calendar. year); system. err. println (y); // output 2 // calendar comparison: Before (), after (), equals (), compareto (). try {string starttime = "12:45:39"; string endtime = "12:45:40"; simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS "); date startdate = SDF. parse (starttime); Date enddate = SDF. parse (endtime); calendar start = calendar. getinstance (); calendar end = calendar. getinstance (); start. settime (startdate); end. settime (enddate); If (start. before (end) {system. err. println ("the start time is earlier than the end time");} else if (start. after (end) {system. err. println ("the start time is later than the end time");} else if (start. equals (end) {system. err. println ("Start time equals End Time");}/** start <End returns-1 * Start = end returns 0 * Start> end returns 1 */int count = start. compareto (end); system. err. println (count); // Add (): add or subtract the specified start time for a given calendar field. add (calendar. year,-3); system. err. println ("original time:" + starttime); system. err. println ("time after add:" + SDF. format (start. gettime (); // tostring (): converts the string to system. err. println (start. tostring ();} catch (exception e) {}} // common attribute public static void param () {date = new date (); Calendar c = calendar. getinstance (); C. settime (date); // calendar. year: The Year in the date int year = C. get (calendar. year); // calendar. month: the month in the date. You need to add 1 int mounth = C. get (calendar. month) + 1; // calendar. date: day int day = C. get (calendar. date); // calendar. hour: The hour in the date (in 12-hour format) int hour = C. get (calendar. hour); // calendar. hour_of_day: 24-hour int hour_of_day = C. get (calendar. hour_of_day); // calendar. minute: minute int minute = C. get (calendar. minute); // calendar. second: Second int second = C. get (calendar. second); system. err. println (Year + "-" + mounth + "-" + day + "" + hour + ":" + minute + ":" + second); // calendar. week_of_year: number of stars in the current year int week_of_year = C. get (calendar. week_of_year); // calendar. week_of_month: number of stars in the current month int week_of_month = C. get (calendar. week_of_month); // calendar. day_of_year: The day in the current year int day_of_year = C. get (calendar. day_of_year); // calendar. day_of_month: The day in the current month int day_of_month = C. get (calendar. day_of_month); // calendar. day_of_week: the day of the current week (Sunday indicates the first day, Saturday indicates the seventh day) int day_of_week = C. get (calendar. day_of_week); // calendar. day_of_week_in_month: The Week in the current month int day_of_week_in_month = C. get (calendar. day_of_week_in_month); try {simpledateformat format = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); Date ampm = format. parse ("21:59:59"); Calendar cc = calendar. getinstance (); CC. settime (ampm); // am_pm: whether hour is before or after noon, 0 is returned before noon, and is returned at noon (including) then 1 int am_pm = Cc is returned. get (calendar. am_pm);} catch (exception e ){}}}

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.