/ * * Datecalculate.java, Created on 2005-3-18 * Copyright 2005 AAA Information Technology Co. Ltd. All Rights Reserved. * * Made Date: 2005-3-18 * Modify resume: * * / package com.bjb. XYH.UTIL; Import Java.text.Decimalformat; // Import Java.text.Parsexception; Import Java.Text.SIMPLEDATEFORMAT; Import Java.util.Calendar; Import Java.util.date; / ** * Class Description: Youdate Date Calculation Class, free expansion * @Author xieyh lamp5w@yahoo.com.cn * @version build 2005-3-18 * * / public class datecalculate {/ ** Note the date when Grid and Confuciors Different * / private static transient int Gregoriancutoveryear = 1582; / ** Monthly day number * / private static final int [] days_p_month_ly = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; / ** Monthly day * / private static final int [] days_p_month_cy = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; / ** Represents the year, month, Japan * / private static final int y = 0, m = 1, d = 2; / ** Participate in * / private int [] YMD = null; / ** * check Whether the incoming parameter is legal date * @Param Date * @throws illegalgumentException * / Public static void validate (String Date) throws illegalargumentException {int [] (] (ymd [m] == 0 || ymd [m]> 12) {throw new illegalargumentException ("Month Numerical Error") } If (true == isleApyear (YMD [0])) {if (YMD [D] == 0 || YMD [D]> DAYS_P_MONTH_LY [YMD [M] -1]) {throw new illegalgumentException ("Date Numerical Error ");}} else {if (YMD [D] == 0 || YMD [D]> DAYS_P_MONTH_CY [YMD [M] -1]) {throw new illegalgumentException (" Date Numerical Error ");
}}}} / ** * Check if the year represents the year represents the year * @Param Year * @return * / public static boolean isleApyear (int year) {returnies> = Gregoriancutoveryear? ((Year% 4 == 0) ) && (Year% 100! = 0) || (Year% 400 == 0))): // Gregorian (Year% 4 == 0); // julian} / ** * Date plus 1 day, pay attention Here, there is no 10-day 10 days of Confucian calendar and Grid hypothesis * @Param Year * @Param Month * @Param Day * @return * / private int [] addoneday (int year, int month, int day) {i (iSleApyear) {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;} / ** * calculated date addition * @Param Date * @Param days * @return * / public string adddaysbyloP in a loop mode * @param Date String Date, Int Days) {Validate (Date); int [] ymd = splitymd (date); for (int i = 0; i Note that the 10th day of the Confucian calendar and the Grid passage time * @Param Year * @Param Month * @Param Day * @return * / private int [] reduceoneday (int year, int month, int day) { IF (IsleApyear) {day-- {MONTH -; IF (Month <1) {year--; month = 12;} day = days_p_month_ly [month -1];} } else {day ---; if (day <= 0) {MONTH -; IF (Month <1) {year--; month = 12;} day = days_p_month_cy [month -1];}} int [] YMD = {Year, Month, Day}; Return YMD;} / ** * Calculate Date subtraction in a loop * @Param Date * @Param Days * @return * / public string reducedaysbyloop (String Date, Int days) {Validate Date); int [] YMD = SPLITYMD (date); for (int i = 0; i throws IllegalArgumentException * / public String addDays (String date, int days) throws IllegalArgumentException {validate (date); ymd = splitYMD (date); if (isLeapYear (ymd [Y])) {ymd [D] = days; if ( YMD [D]> DAYS_P_MONTH_LY [YMD [] -1]) {YMD [M] ; YMD [D] = YMD [D] - DAYS_P_MONTH_LY [YMD [M] -1-1]; if (YMD [M ]> 12) {YMD [M] - = 12; YMD [Y] ;} IF (YMD [D]> DAYS_P_MONTH_LY [YMD [M] -1]) {adddays (formatyear (YMD [Y]) FormatMonthday (YMD [M]) FormatMontHDAY (DAYS_P_MONTH_LY [YMD [M] -1]), YMD [D] - DAYS_P_MONTH_LY [YMD [M] -1]);}}} else {YMD [D] = days; if (YMD [D]> DAYS_P_MONTH_CY [YMD [M] -1]) {YMD [M] ; YMD [D] = YMD [D] - Days_P_MONTH_CY [ YMD [M] -1-1]; if (YMD [M]> 12) {YMD [M] - = 12; YMD [Y] ;} IF (YMD [D]> DAYS_P_MONTH_CY [YMD [M] - 1]) {AddDays (YMD [Y]) FormatMontHDAY (YMD [Month_Cy [YMD [M] -1]), YMD [D] - Days_P_MONTH_CY [YMD [M] -1]) ;}}}}} Return Formatyear (YMD [Y]) FormatMontHDAY (YMD [D]);} / ** * Specify the date of the specified number of days * @Param Date * @Param Days * @ return * @throws IllegalArgumentException * / public String reduceDays (Date date, int days) throws IllegalArgumentException {return reduceDays (formatDate (date), days);} / ** * specified date minus a specified number of days of operation * @param date * @param days * @return * @throws IllegalArgumentException * / public String reduceDays (String date, int days) throws IllegalArgumentException {validate (date); ymd = splitYMD (date); if (isLeapYear (ymd [Y])) {ymd [ D] - = days; if (YMD [D] <= 0) {YMD [M] -; IF (YMD [M] <1) {YMD [M] = 12; YMD [Y] -;} YMD [D] = YMD [D] DAYS_P_MONTH_LY [YMD [M] -1]; IF (YMD [D] <= 0) {ReducedAys (Formatyear (YMD [Y]) FormatMontHDAY (YMD [M]) FormatMonthday (1), ABS (YMD [D] - 1)); }}} Else {ymd [d] - = days; if (YMD [D] <= 0) {YMD [M] -; IF (YMD [M] <1) {YMD [M] = 12; YMD [Y] -;} YMD [D] = YMD [D] DAYS_P_MONTH_CY [YMD [M] -1]; IF (YMD [D] <= 0) {ReducedAys (Formatyear (YMD [Y]) FormatMonthday ( YMD [M]) FormatMontHDAY (1), ABS (YMD [D] - 1));}}} Return Formatyear (YMD [Y]) FormatMontHDAY (YMD [M]) FormatMontHDay (YMD [D]);} / ** * Format a date string * @Param Date * @Return * / public static string formatdate (Date Date) {SimpleDateFormat SDF = New SimpleDateFormat ("YYYYMMDD"); Return SDF .format (date);} / ** * Segment a string of the representative date to the representative of the monos of the monarch * @Param Date * @return * / public static int [] splitymd (string date) {int [】 YMD = {0, 0, 0}; YMD [Y] = INTEGER.PARSEINT (Date.Substring (0, 4)); YMD [M] = INTEGER.PARSEINT (Date.Substring (4, 6)); YMD [D ] = INTEGER.PARSEINT (Date.Substring (6, 8)); return ymd;} / ** * will make up two months or dates to 2 * @Param Decimal * @return * / public static string formatMonthday ( INT decimal) {decimalformat df = new decimalformat ("00"); returnif.format (decimal);} / ** * Adding less than four years of complement to four * @Param decimal * @return * / public static string Formatyear (int decimal) {decimalformat DF = new decimalformat ("0000"); RETU RN DF.FORMAT (DECIMAL);} / ** * Take absolute value * @Param Num * @return * / public static int ABS (INT NUM) {RETURN (NUM> 0)? Num: -num;} / * * * Test Main function * @Param args * / public static void main (string [] args) throws exception {string currdate = "20001231"; string currdate2 = "20001231"; int days = -36600; Date Date = NULL; DateCalculate DC = New Datecalculate (); SimpleDateFormat SDF = New SimpleDateFormat ("YYYYMMDD"); DATE = SDF.PARSE (currdate2);