JAVA time and date processing class, mainly used to traverse every day between two dates ., Java every day

Source: Internet
Author: User

JAVA time and date processing class, mainly used to traverse every day between two dates ., Java every day

/***** File name: AccountDate. java ** Creation Time: ** Email: ** @ 163.com */import java. text. decimalFormat; import java. text. parseException; import java. text. simpleDateFormat; import java. util. arrayList; import java. util. date; import java. util. list; public class AccountDate {private static transient int gregorianCutoverYear = 1582;/** Number of days per month in a leap year */private static final int [] DAYS_P_MONTH_LY = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};/** days of each month in a non-leap year */private static final int [] DAYS_P_MONTH_CY = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /** indicates the year, month, and day in the array */private static final int Y = 0, M = 1, D = 2; /*** split the String representing the date into an integer array representing the year, month, and day * @ param date * @ return */public static int [] splitYMD (String date) {date = date. replace ("-", ""); int [] ymd = {0, 0, 0}; ymd [Y] = Integer. parseInt (date. substrin G (0, 4); ymd [M] = Integer. parseInt (date. substring (4, 6); ymd [D] = Integer. parseInt (date. substring (6, 8); return ymd ;} /*** check whether the input parameter represents a leap year * @ param year * @ return */public static boolean isLeapYear (int year) {return year> = gregorianCutoverYear? (Year % 4 = 0) & (year % 100! = 0) | (year % 400 = 0): (year % 4 = 0 );} /*** date plus 1 day * @ param year * @ param month * @ param day * @ return */private static int [] addOneDay (int year, int month, int day) {if (isLeapYear (year) {day ++; if (day> DAYS_P_MONTH_LY [month-1]) {month ++; if (month> 12) {year ++; month = 1 ;}day = 1 ;}} else {day ++; if (day> DAYS_P_MONTH_CY [month-1]) {month ++; if (month> 12) {year ++; month = 1 ;}day = 1 ;}} int [] ymd = {year, month, day }; return ymd ;} /*** add less than two months or dates to two places * @ param decimal * @ return */public static String formatMonthDay (int decimal) {DecimalFormat df = new DecimalFormat ("00"); return df. format (decimal);}/*** set the year of less than four digits to four digits * @ param decimal * @ return */public static String formatYear (int decimal) {DecimalFormat df = new DecimalFormat ("0000"); return df. format (decimal );} /*** calculate the number of days between two dates * @ param begin * @ param end * @ return * @ throws ParseException */public static long countDay (String begin, String end) {SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd"); Date beginDate, endDate; long day = 0; try {beginDate = format. parse (begin); endDate = format. parse (end); day = (endDate. getTime ()-beginDate. getTime ()/(24x60x60*1000);} catch (ParseException e) {e. printStackTrace ();} return day ;} /*** calculate the date cyclically * @ param beginDate endDate * @ param days * @ return */public static List <String> getEveryday (String beginDate, String endDate) {long days = countDay (beginDate, endDate); int [] ymd = splitYMD (beginDate); List <String> everyDays = new ArrayList <String> (); everyDays. add (beginDate); for (int I = 0; I <days; I ++) {ymd = addOneDay (ymd [Y], ymd [M], ymd [D]); everyDays. add (formatYear (ymd [Y]) + "-" + formatMonthDay (ymd [M]) + "-" + formatMonthDay (ymd [D]);} return everyDays ;} public static void main (String [] args) {List <String> list = AccountDate. getEveryday ("2008-08-29", "2008-09-02"); for (String result: list) {System. out. println (result );}}}

 

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.