Share: Genericcalendar Tool class

Source: Internet
Author: User

During the development of the project, the "customer" presents the need to load the latest notification information sooner or later, depending on the date. Think of a lot of methods, and finally wrote the Org.warnier.zhang.utils.GenericCalendar class to meet customer needs, the implementation process of reference to the Java.util.Calendar class CompareTo () method;

The best thing about the Genericcalendar class is the method Getdaysof () that calculates the number of days from A.D. January 1, 1, and the code is as follows:

1     /**2 * Calculate the number of days from the current date from January 1, 1 A.D.;3      * @paramCalendar4      * @returnthe number of days from January 1, 1 of the current date;5      */6     Private intgetdaysof (Genericcalendar calendar) {7         int[] days = {8{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},9{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},Ten         }; One          A         intresult =Calendar.currentday; -          for(inti = 1; i < calendar.currentyear; i + +){ -result = result + 365 + (((i% 4 = = 0 && i% = 0) | | I% 400 = 0)? 1:0); the         } -         intLeap = ((calendar.currentyear% 4 = = 0 && calendar.currentyear%! = 0) | | calendar.currentyear% 400 = 0)? 1:0; -          for(intj = 1; J < Calendar.currentmonth; J + +){ -Result + =Days[leap][j]; +         } -         returnresult; +}

  A two-dimensional array and leap year's judgment flag leap to determine whether February is 28 days or 29 days.

The complete code is as follows:

1  Packageorg.warnier.zhang.utils;2 3 ImportJava.util.Calendar;4 ImportJava.util.GregorianCalendar;5 6 /**7 * Calendar of tool classes, mainly to achieve two date comparison function;8  * @authorWarnier-zhang9  *Ten  */ One  Public classgenericcalendar{ A      Public intCurrentYear; -      Public intCurrentmonth; -      Public intCurrentday; the      -     /** - * The calendar is initialized according to the current date of the year, month and day; -      */ +      PublicGenericcalendar () { -Calendar Calendar =NewGregorianCalendar (); +CurrentYear =Calendar. Year; ACurrentmonth =Calendar. MONTH; atCurrentday =Calendar. Day_of_month; -     } -      -     /** - * The calendar is initialized according to the year, month and day of the specified date; -      * @paramCurrentYear in      * @paramCurrentmonth -      * @paramCurrentday to      */ +      PublicGenericcalendar (intCurrentYear,intCurrentmonth,intcurrentday) { -          This. CurrentYear =CurrentYear; the          This. Currentmonth =Currentmonth; *          This. Currentday =Currentday; $     }Panax Notoginseng      -     /** the * According to the date of the year, month, day, compare two dates; +      * @paramAnothercalendar A      * @returncompares whether the current date is later than the reference date, returns 1, indicates the delay, returns 0, indicates the same; Returns-1, indicating early; the      */ +      Public intcompareTo (Genericcalendar anothercalendar) { -         returnCompareTo (Getdaysof (Anothercalendar)); $     } $      -     /** - * Calculate the number of days from the current date from January 1, 1 A.D.; the      * @paramCalendar -      * @returnthe number of days from January 1, 1 of the current date;Wuyi      */ the     Private intgetdaysof (Genericcalendar calendar) { -         int[] days = { Wu{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, -{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, About         }; $          -         intresult =Calendar.currentday; -          for(inti = 1; i < calendar.currentyear; i + +){ -result = result + 365 + (((i% 4 = = 0 && i% = 0) | | I% 400 = 0)? 1:0); A         } +         intLeap = ((calendar.currentyear% 4 = = 0 && calendar.currentyear%! = 0) | | calendar.currentyear% 400 = 0)? 1:0; the          for(intj = 1; J < Calendar.currentmonth; J + +){ -Result + =Days[leap][j]; $         } the         returnresult; the     } the      the     /** - * Compare two dates according to the number of days from January 1, 1 A.D. in      */ the     Private intCompareTo (intDays ) { the         intThisdays = Getdaysof ( This); About         return(Thisdays > Days)? 1: (Thisdays = = days)? 0:-1;  the     } the      the}

Share: Genericcalendar Tool class

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.