飞艳小屋

程序--人生--哲学___________________欢迎艳儿的加入

BlogJava 首页 新随笔 联系 聚合 管理
  52 Posts :: 175 Stories :: 107 Comments :: 0 Trackbacks

package jp.co.smile.common.utility;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

//*************************************************************************
/**
 * 擔晅憖嶌偺嫟捠娭悢僋儔僗偱偡丅<P>
 * 偙偺僋儔僗偺儊僜僢僪偼慡偰static儊僜僢僪偵側偭偰偄傑偡偺偱丄
 * 僀儞僗僞儞僗壔偣偢偵巊梡偟傑偡丅<BR>
 * <B>巊梡椺丗</B>
 * <BLOCKQUOTE><PRE>
 *  //擇偮偺擔晅娫偺擔悢傪庢摼偟傑偡丅
 *  try {
 *    int returnDays = DateUtility.getDayCount("20010311", "20010411");
 *  } catch(Exception e) {
 *    //僄儔乕張棟傪峴偆
 *  }
 * </BLOCKQUOTE></PRE>
 *
 * @author  NECST Software Engineering Promotion Center
 * @version $Revision$ $Date$
 * @since SWEC Parts Release 1.0
 */
//*************************************************************************
public class DateUtility {

  //***********************************************************************
  //public僼傿乕儖僪
  //***********************************************************************

  /** 擭係寘寧俀寘擔俀寘傪昞偡抣偱偡 */
  public static final int YYYYMMDD = 1;

  /** 擭係寘寧俀寘傪昞偡抣偱偡 */
  public static final int YYYYMM = 2;

  /** 擭係寘傪昞偡抣偱偡 */
  public static final int YYYY = 3;

  /** 擭俀寘寧俀寘擔俀寘傪昞偡抣偱偡 */
  public static final int YYMMDD = 4;

  /** 擭俀寘寧俀寘傪昞偡抣偱偡 */
  public static final int YYMM = 5;

  /** 擭俀寘傪昞偡抣偱偡 */
  public static final int YY = 6;

  /** 寧俀寘擔俀寘傪昞偡抣偱偡 */
  public static final int MMDD = 7;

  /** 寧俀寘傪昞偡抣偱偡 */
  public static final int MM = 8;

  /** 擔俀寘傪昞偡抣偱偡 */
  public static final int DD = 9;

  /** 僙僷儗乕僞偵乽/乿傪巊偆応崌偺抣偱偡 */
  public static final int SLASH = 1;

  /** 僙僷儗乕僞偵擔杮岅傪巊偆応崌偺抣偱偡 */
  public static final int JAPANESE = 2;

  /** 僙僷儗乕僞傪巊傢側偄応崌偺抣偱偡 */
  public static final int NONE = 3;

  /** 嵟彫擔晅 */
  public static final Date MIN_VALUE = createDate(1970, 1, 1);

  /** 嵟戝擔晅 */
  public static final Date MAX_VALUE = createDate(9999, 12, 31);

  //*************************************************************************
  //private僼傿乕儖僪
  //*************************************************************************

  /** 擔傪妱傝弌偡悢抣(24*60*60*1000) */
  private static final long  DATE_VALUE = 86400000;

  //*************************************************************************
  //僐儞僗僩儔僋僞
  //*************************************************************************

  //*************************************************************************
  //public儊僜僢僪
  //*************************************************************************
  //*************************************************************************
  /**
   * 僔僗僥儉擔晅丒帪崗傪庢摼偟傑偡丅
   *
   * @param     argPattern    1丗'YYYY-MM-DD HH24:MI:SS'宍幃偱曉偡<BR>
   *                           2丗'YYYYMMDD'宍幃偱曉偡<BR>
   *                           3丗'HH24MISS'宍幃偱曉偡<BR>
   * @return    argPattern偱巜掕偝傟偨宍幃偺擔晅丒帪崗<BR>
   */
  //*************************************************************************
  public static String getTimeStamp(int argPattern) {

   String sTimeStamp = "";

    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("JST"));
    DecimalFormat decimalFormat = new DecimalFormat();

    decimalFormat.applyPattern("0000");
    String sYear = decimalFormat.format(calendar.get(Calendar.YEAR));

    decimalFormat.applyPattern("00");
    String sMonth = decimalFormat.format(calendar.get(Calendar.MONTH)+1 );
    String sDay = decimalFormat.format(calendar.get(Calendar.DATE));
    String sHh = decimalFormat.format(calendar.get(Calendar.HOUR_OF_DAY));
    String sMm = decimalFormat.format(calendar.get(Calendar.MINUTE));
    String sSs = decimalFormat.format(calendar.get(Calendar.SECOND));

    switch (argPattern) {
      case 1:    //'YYYY-MM-DD HH24:MI:SS'宍幃偱曉偡
        sTimeStamp = sYear + "-" + sMonth + "-" + sDay + " " + sHh + ":" + sMm + ":" + sSs;
        break;
      case 2:    //'YYYYMMDD'宍幃偱曉偡
        sTimeStamp = sYear + sMonth + sDay;
        break;
      case 3:    //'HH24MISS'宍幃偱曉偡
        sTimeStamp = sHh + sMm + sSs;
        break;
      default:
        throw new IllegalArgumentException("argPattern is error");
    }

    return sTimeStamp;
  }

  //*************************************************************************
  /**
   * 巜掕偝傟偨擔晅(惣楋)偺懨摉惈傪僠僃僢僋偟傑偡丅<BR>
   *
   * @param   argDate   擭寧擔乮YYYYMMDD乯
   * @return  true:惓忢丄false:俶俧
   */
  //*************************************************************************
  public static boolean isValidateDate(String argDate) {
    //悢抣宆偱丄擭寧擔傪庢摼
    int yearNum = 0;
    int monthNum = 0;
    int dayNum = 0;

    if (argDate == null || argDate.length() != 8) return false;

    try {

      yearNum = Integer.parseInt(argDate.substring(0,4));
      monthNum = Integer.parseInt(argDate.substring(4,6)) - 1;
      dayNum = Integer.parseInt(argDate.substring(6,8));

      //懨摉惈僠僃僢僋
      Calendar calendar = new GregorianCalendar(yearNum, monthNum, dayNum);
      calendar.setLenient(false);
      calendar.getTime();

    } catch (Throwable ex) {
      return false;
    }
    return true;

  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨擔晅(僗儔僢僔儏崬傒惣楋)偺懨摉惈傪僠僃僢僋偟傑偡丅<BR>
   * 擭丄寧丄擔偑僗儔僢僔儏埲奜偺婰崋偱嬫愗傜傟偰偄偰傕栤戣偁傝傑偣傫丅
   *
   * @param  argDate   擔晅乮YYYY/MM/DD乯
   * @return true:惓忢丄false:俶俧
   */
  //***************************************************************************
  public static boolean isValidateDateSlash(String argDate) {

    //悢抣宆偱丄擭寧擔傪庢摼
    int yearNum = 0;
    int monthNum = 0;
    int dayNum = 0;

    if (argDate == null || argDate.length() != 10) return false;

    try {
      yearNum = Integer.parseInt(argDate.substring(0,4));
      monthNum = Integer.parseInt(argDate.substring(5,7)) - 1;
      dayNum = Integer.parseInt(argDate.substring(8,10));

      //懨摉惈僠僃僢僋
      Calendar calendar = new GregorianCalendar(yearNum, monthNum, dayNum);
      calendar.setLenient(false);
      calendar.getTime();

    } catch (Throwable ex) {
      return false;
    }
    return true;
  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨帪崗(帪暘)偺懨摉惈傪僠僃僢僋偟傑偡丅<BR>
   *
   * @param argTime   帪暘乮HHMM乯
   * @return true:惓忢丄false:俶俧
   */
  //***************************************************************************
  public static boolean isValidateTime(String argTime) {

    if (argTime == null || argTime.length() != 4) {
      return false;  //堷悢偺帪崗偑晄惓側応崌false傪栠傝抣偲偡傞
    }

    try {
      int intArgH = Integer.parseInt(argTime.substring(0, 2));
      int intArgM = Integer.parseInt(argTime.substring(2));

      //懨摉惈僠僃僢僋
      Calendar calendar = GregorianCalendar.getInstance();
      calendar.set(Calendar.HOUR_OF_DAY, intArgH);
      calendar.set(Calendar.MINUTE, intArgM);
      if (calendar.get(Calendar.HOUR_OF_DAY) == intArgH
            && calendar.get(Calendar.MINUTE) == intArgM) {
      } else {
        return false;
      }

    } catch (NumberFormatException ex) {
      return false;
    }
    return true;

  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨擭寧擔偺梛擔傪庢摼偟傑偡丅<BR>
   * 擔晅偺懨摉惈僠僃僢僋偼峴偄傑偣傫丅僠僃僢僋屻偺擔晅傪僙僢僩偟偰偔偩偝偄丅
   *
   * @param argDate   擭寧擔乮YYYYMMDD乯
   * @return    梛擔掕悢
   *           <TABLE BORDER="1">
   *           <TR><TD>Calendar.SUNDAY</TD><TD> 擔</TD></TR>
   *           <TR><TD>Calendar.MONDAY</TD><TD> 寧</TD></TR>
   *           <TR><TD>Calendar.TUESDAY</TD><TD> 壩</TD></TR>
   *           <TR><TD>Calendar.WEDNESDAY</TD><TD> 悈</TD></TR>
   *           <TR><TD>Calendar.THURSDAY</TD><TD> 栘</TD></TR>
   *           <TR><TD>Calendar.FRIDAY</TD><TD> 嬥</TD></TR>
   *           <TR><TD>Calendar.SATURDAY</TD><TD> 搚</TD></TR>
   *           </TABLE>
   */
  //***************************************************************************
  public static int getDayOfWeek(String argDate) {

    if (argDate.length() != 8) {
      throw new IllegalArgumentException("argDate length != 8");
    }

    int intArgY = Integer.parseInt(argDate.substring(0, 4));
    int intArgM = Integer.parseInt(argDate.substring(4, 6)) - 1;
    int intArgD = Integer.parseInt(argDate.substring(6));
    //堷悢偱梌偊傜傟偨擔晅傪愝掕
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(intArgY, intArgM, intArgD);
    //梛擔偺庢摼
    int returnDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

    return returnDayOfWeek;
  }

  //***************************************************************************
  /**
   * 巜掕擔晅偐傜偺憡懳廡擔晅偺廤崌傪曉偟傑偡丅<BR>
   * 擔晅偺懨摉惈僠僃僢僋偼峴偄傑偣傫丅僠僃僢僋屻偺擔晅傪僙僢僩偟偰偔偩偝偄丅
   *
   * @param   argDay 巜掕擔晅(YYYYMMDD宍幃)
   * @param   argRelative 巜掕擔晅偐傜偺憡懳廡(0, 1, 2, ...)
   * @param   argStart 廡偺奐巒擔(0=擔丄1=寧丄2=壩丄3=悈丄4=栘丄5=嬥丄6=搚)
   * @return  摉奩廡偺擔晅偺廤崌傪YYYYMMDD宍幃偱曉偟傑偡丅
   */
  //***************************************************************************
  public static String[] getWeek(String argDay, int argRelative, int argStart) {

    if (argDay.length() != 8) {
      throw new IllegalArgumentException("argDay length != 8");
    }
    if (argStart < 0 || argStart > 6) {
      throw new IllegalArgumentException("argStart is error");
    }

    Calendar date = Calendar.getInstance();
    date.clear();
      date.set(Integer.parseInt(formatDate(argDay, DateUtility.YYYY, DateUtility.NONE)),
               Integer.parseInt(formatDate(argDay, DateUtility.MM, DateUtility.NONE)) - 1,
      Integer.parseInt(formatDate(argDay, DateUtility.DD, DateUtility.NONE)));

    int dyaow = date.get(Calendar.DAY_OF_WEEK);
    int offset;
    int past;

    switch (dyaow) {
      case Calendar.SUNDAY:
        offset = 0;
        break;
      case Calendar.MONDAY:
        offset = 1;
        break;
      case Calendar.TUESDAY:
        offset = 2;
        break;
      case Calendar.WEDNESDAY:
        offset = 3;
        break;
      case Calendar.THURSDAY:
        offset = 4;
        break;
      case Calendar.FRIDAY:
        offset = 5;
        break;
      case Calendar.SATURDAY:
        offset = 6;
        break;
      default:
        offset = 0;
        break;
      }

    Calendar days[] = new Calendar[7];

    if (offset >= argStart) {
      past = offset - argStart;
    } else {
      past = offset + 7 - argStart;
    }

    date.add(Calendar.DATE, (-1) * past);

    days[0] = (Calendar)date.clone();
    for (int iCounter = 1; iCounter < 7; iCounter++) {
      date.add(Calendar.DATE, 1);
      days[iCounter] = (Calendar)date.clone();
    }

    String retdays[] = new String[7];
    int year;
    int month;
    int day;

    for (int iCounter = 0; iCounter < 7; iCounter++) {
      days[iCounter].add(Calendar.DATE, 7 * argRelative);
      year = days[iCounter].get(Calendar.YEAR);
      month = days[iCounter].get(Calendar.MONTH) + 1;
      day = days[iCounter].get(Calendar.DATE);
      retdays[iCounter] = toCharacter(year) + toCharacter(month) + toCharacter(day);
    }

    return retdays;
  }

  //****************************************************************************
  /**
   *  寧偺嵟廔擔傪曉媝偟傑偡丅<br>
   *
   *  @param argYear  擭乮int宆乯
   *  @param argMonth 寧乮int宆乯
   *
   *  @return 嵟廔擔乮int宆乯
   */
  //****************************************************************************
  public static int getLastDayOfMonth(int argYear , int argMonth) {

    if (argYear <= 0){
      throw new IllegalArgumentException("argYear <= 0");
    }

    switch (argMonth){
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        return 31;
      case 4:
      case 6:
      case 9:
      case 11:
        return 30;
      case 2:
        if(DateUtility.isLeapYear(argYear)){
          return 29;
        }else{
          return 28;
        }
      default:
        throw new IllegalArgumentException("argMonth is error");
    }
  }

  //****************************************************************************
  /**
   *  寧偺嵟廔擔傪曉媝偟傑偡丅<br>
   *
   *  @param argYear  擭乮String宆乯
   *  @param argMonth 寧乮String宆乯
   *
   *  @return 嵟廔擔乮String宆乯
   */
  //****************************************************************************
  public static String getLastDayOfMonth(String argYear , String argMonth) {

    //堷悢偺int宆傊偺曄姺
    int iYear = Integer.parseInt(argYear);
    int iMonth = Integer.parseInt(argMonth);
    return Integer.toString(getLastDayOfMonth(iYear,iMonth));
  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨擔晅(惣楋)偐傜偦偺寧枛傑偱偺擔悢傪寁嶼偟傑偡丅<BR>
   * 擔晅偺懨摉惈僠僃僢僋偼峴偄傑偣傫丅僠僃僢僋屻偺擔晅傪僙僢僩偟偰偔偩偝偄丅
   *
   * @param argDate   擭寧擔乮YYYYMMDD乯
   * @return   擔悢<BR>
   */
  //***************************************************************************
  public static int getLeftDaysOfMonth(String argDate) {

    if (argDate.length() != 8) {
      throw new IllegalArgumentException("argDate length != 8");
    }

    int intArgY = Integer.parseInt(argDate.substring(0, 4));
    int intArgM = Integer.parseInt(argDate.substring(4, 6)) - 1;
    int intArgD = Integer.parseInt(argDate.substring(6));
    //堷悢偱梌偊傜傟偨擔晅傪愝掕
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(intArgY, intArgM, intArgD);
    //枛擔偺庢摼
    int intMaxDayOfMonth = calendar.getActualMaximum(Calendar.DATE);

    return (intMaxDayOfMonth - calendar.get(Calendar.DATE));
  }

  //***************************************************************************
  /**
   * 婎揰偲側傞擔晅偺俶擔屻偺擔晅傪寁嶼偟傑偡丅<BR>
   * 擔晅偺懨摉惈僠僃僢僋偼峴偄傑偣傫丅僠僃僢僋屻偺擔晅傪僙僢僩偟偰偔偩偝偄丅
   *
   * @param argStartDate  擭寧擔(YYYYMMDD乯
   * @param argNum        擔悢
   * @return 庢摼擔晅乮YYYYMMDD乯<BR>
   */
  //***************************************************************************
  public static String getAfterDays(String argStartDate, int argNum) {

    if (argStartDate.length() != 8) {
      throw new IllegalArgumentException("argDate length != 8");
    }

    int intArgStartY = Integer.parseInt(argStartDate.substring(0, 4));
    int intArgStartM = Integer.parseInt(argStartDate.substring(4, 6)) - 1;
    int intArgStartD = Integer.parseInt(argStartDate.substring(6));

    //堷悢偱梌偊傜傟偨奐巒擔晅傪愝掕
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(intArgStartY, intArgStartM, intArgStartD);
    //俶擔屻偺擔晅傪庢摼
    calendar.add(Calendar.DATE, argNum);

    int returnY = calendar.get(Calendar.YEAR);
    int returnM = calendar.get(Calendar.MONTH) + 1;
    int returnD = calendar.get(Calendar.DATE);
    int returnAfterDays = returnY * 10000 + returnM * 100 + returnD;

    return String.valueOf(returnAfterDays);

  }

  //***************************************************************************
  /**
   * 婎揰偲側傞擔晅偺俶儢寧屻偺擔晅傪寁嶼偟傑偡丅<BR>
   * 擔晅偺懨摉惈僠僃僢僋偼峴偄傑偣傫丅僠僃僢僋屻偺擔晅傪僙僢僩偟偰偔偩偝偄丅
   *
   * @param argDate  擭寧擔(YYYYMMDD乯
   * @param argNum   寧悢
   * @return 庢摼擔晅乮YYYYMMDD乯
   */
  //***************************************************************************
  public static String getAfterMonth(String argDate, int argNum) {

    if (argDate.length() != 8) {
      throw new IllegalArgumentException("argDate length != 8");
    }

    int intArgStartY = Integer.parseInt(argDate.substring(0, 4));
    int intArgStartM = Integer.parseInt(argDate.substring(4, 6)) - 1;
    int intArgStartD = Integer.parseInt(argDate.substring(6));
    //堷悢偱梌偊傜傟偨奐巒擔晅傪愝掕
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(intArgStartY, intArgStartM, intArgStartD);
    //俶儢寧屻偺擔晅傪庢摼
    calendar.add(Calendar.MONTH, argNum);

    int returnY = calendar.get(Calendar.YEAR);
    int returnM = calendar.get(Calendar.MONTH) + 1;
    int returnD = calendar.get(Calendar.DATE);
    int returnAfterMonth = returnY * 10000 + returnM * 100 + returnD;

    return String.valueOf(returnAfterMonth);

  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨帪崗(惣楋擭寧擔帪暘)偵帪娫(帪暘)傪壛嶼傑偨偼尭嶼偟偨
   * 帪崗(惣楋擭寧擔帪暘)傪寁嶼偟傑偡丅<BR>
   * 擔晅偺懨摉惈僠僃僢僋偼峴偄傑偣傫丅僠僃僢僋屻偺擔晅傪僙僢僩偟偰偔偩偝偄丅
   *
   * @param argDate  奐巒擭寧擔帪暘(YYYYMMDDHHMM乯
   * @param argNum   帪暘(HHMM)
   * @return 庢摼擭寧擔帪暘(YYYYMMDDHHMM乯
   */
  //***************************************************************************
  public static String getReplyTime(String argDate, String argNum) {

    if (argDate.length() != 12) {
      throw new IllegalArgumentException("argDate length != 12");
    }
    if (argNum.length() != 4) {
      throw new IllegalArgumentException("argNum length != 4");
    }

    int intArgStartY = Integer.parseInt(argDate.substring(0, 4));
    int intArgStartM = Integer.parseInt(argDate.substring(4, 6)) - 1;
    int intArgStartD = Integer.parseInt(argDate.substring(6, 8));
    int intArgStartH = Integer.parseInt(argDate.substring(8, 10));
    int intArgStartMin = Integer.parseInt(argDate.substring(10));
    //堷悢偱梌偊傜傟偨奐巒擔晅傪愝掕
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(intArgStartY, intArgStartM, intArgStartD, intArgStartH, intArgStartMin);

    int intArgH = Integer.parseInt(argNum.substring(0, 2));
    int intArgMin = Integer.parseInt(argNum.substring(2));
    //俶帪娫屻偺帪崗傪庢摼
    calendar.add(Calendar.HOUR_OF_DAY, intArgH);
    calendar.add(Calendar.MINUTE, intArgMin);

    long returnY = (long)calendar.get(Calendar.YEAR);
    long returnM = (long)calendar.get(Calendar.MONTH) + 1;
    long returnD = (long)calendar.get(Calendar.DATE);
    long returnH = (long)calendar.get(Calendar.HOUR_OF_DAY);
    long returnMin = (long)calendar.get(Calendar.MINUTE);
    long returnReplyTime = returnY * 100000000 + returnM * 1000000
                    + returnD * 10000 + returnH * 100  + returnMin;
    return String.valueOf(returnReplyTime);

  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨婜娫偺擔悢傪庢摼偟傑偡丅<BR>
   *
   * @param  argStart 奐巒擔乮惣楋乯<BR>
   * @param  argEnd   廔椆擔乮惣楋乯<BR>
   * @return 巜掕婜娫偺擔悢<BR>
   * @see Calendar
   */
  //***************************************************************************
  public static int getDayCount(Calendar argStart, Calendar argEnd) {

    //巜掕偝傟偨婜娫偺擔悢
    long days = 0;

    Calendar start = Calendar.getInstance();
    Calendar end = Calendar.getInstance();

    //尩枾側夝愅偺僠僃僢僋
    start.setLenient(false);
    end.setLenient(false);

    start.set(Calendar.YEAR, argStart.get(Calendar.YEAR));
    start.set(Calendar.MONTH, argStart.get(Calendar.MONTH));
    start.set(Calendar.DATE, argStart.get(Calendar.DATE));

    end.set(Calendar.YEAR, argEnd.get(Calendar.YEAR));
    end.set(Calendar.MONTH, argEnd.get(Calendar.MONTH));
    end.set(Calendar.DATE, argEnd.get(Calendar.DATE));

    //奐巒擔偺帪暘昩偺弶婜壔
    start.clear(Calendar.HOUR_OF_DAY);
    start.clear(Calendar.HOUR);
    start.clear(Calendar.MINUTE);
    start.clear(Calendar.SECOND);

    //廔椆擔偺帪暘昩偺弶婜壔
    end.clear(Calendar.HOUR_OF_DAY);
    end.clear(Calendar.HOUR);
    end.clear(Calendar.MINUTE);
    end.clear(Calendar.SECOND);

   //儈儕昩偱奐巒擔丄廔椆擔偺嵎暘傪媮傔傑偡丅
    days = end.getTime().getTime() - start.getTime().getTime();

    //嵎暘(儈儕昩)傪嵎暘(擔悢)偵曄姺偟傑偡丅
    days = days / DATE_VALUE;

    return (int)days;
  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨婜娫偺擔悢傪庢摼偟傑偡丅<BR>
   *
   * @param  argStartYear  奐巒擔偺擭乮惣楋乯<BR>
   * @param  argStartMonth 奐巒擔偺寧<BR>
   * @param  argStartDay   奐巒擔偺擔<BR>
   * @param  argEndYear    廔椆擔偺擭乮惣楋乯<BR>
   * @param  argEndMonth   廔椆擔偺寧<BR>
   * @param  argEndDay     廔椆擔偺擔<BR>
   * @return 巜掕婜娫偺擔悢<BR>
   * @see Calendar
   */
  //***************************************************************************
  public static int getDayCount(int argStartYear, int argStartMonth, int argStartDay,
                                int argEndYear,   int argEndMonth,   int argEndDay) {

    //奐巒擔丄廔椆擔偺奿擺曄悢
    Calendar start = Calendar.getInstance();
    Calendar end = Calendar.getInstance();

    //尩枾側夝愅偺僠僃僢僋
    start.setLenient(false);
    end.setLenient(false);

    //奐巒擔丄廔椆擔傪奿擺偟傑偡丅
    start.set(argStartYear, argStartMonth - 1, argStartDay);
    end.set(argEndYear, argEndMonth - 1, argEndDay);

    return getDayCount(start, end);
  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨婜娫偺擔悢傪庢摼偟傑偡丅<BR>
   *
   * @param  argStart 奐巒擔乮惣楋乯<BR>
   * @param  argEnd   廔椆擔乮惣楋乯<BR>
   * @return 巜掕婜娫偺擔悢<BR>
   * @see Calendar
   */
  //***************************************************************************
  public static int getDayCount(Date argStart, Date argEnd) {

    //奐巒擔丄廔椆擔偺奿擺曄悢
    Calendar start = Calendar.getInstance();
    Calendar end = Calendar.getInstance();

    //尩枾側夝愅偺僠僃僢僋
    start.setLenient(false);
    end.setLenient(false);

    //奐巒擔丄廔椆擔傪奿擺偟傑偡丅
    start.setTime(argStart);
    end.setTime(argEnd);

    return getDayCount(start, end);
  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨婜娫偺擔悢傪庢摼偟傑偡丅<BR>
   *
   * @param  argStart 奐巒擔乮YYYYMMDD乯
   * @param  argEnd   廔椆擔乮YYYYMMDD乯
   * @return 巜掕婜娫偺擔悢<BR>
   * @see Calendar
   */
  //***************************************************************************
  public static int getDayCount(String argStart, String argEnd) {

    //堷悢偺僠僃僢僋
    if (argStart.trim().length() != 8 ||
        argEnd.trim().length() != 8) {
        throw new IllegalArgumentException("argStart or argEnd length != 8");
    }

    //奐巒擔丄廔椆擔偺奿擺曄悢
    Calendar start = Calendar.getInstance();
    Calendar end = Calendar.getInstance();

    //尩枾側夝愅偺僠僃僢僋
    start.setLenient(false);
    end.setLenient(false);

    //暥帤楍偐傜奐巒擔偺擭丄寧丄擔傪拪弌偟傑偡丅
    int startYear = Integer.parseInt(argStart.substring(0, 4));
    int startMonth = Integer.parseInt(argStart.substring(4, 6));
    int startDay = Integer.parseInt(argStart.substring(6, 8));

    //暥帤楍偐傜廔椆擔偺擭丄寧丄擔傪拪弌偟傑偡丅
    int endYear = Integer.parseInt(argEnd.substring(0, 4));
    int endMonth = Integer.parseInt(argEnd.substring(4, 6));
    int endDay = Integer.parseInt(argEnd.substring(6, 8));

    //僇儗儞僟乕宆偵僙僢僩偟傑偡丅
    start.set(startYear, startMonth - 1, startDay);
    end.set(endYear, endMonth - 1, endDay);

    return getDayCount(start, end);
  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨擇偮偺帪崗(惣楋擭寧擔帪暘)娫偺帪娫(帪暘)傪庢摼偟傑偡丅<BR>
   * 擔晅偺懨摉惈僠僃僢僋偼峴偄傑偣傫丅僠僃僢僋屻偺擔晅傪僙僢僩偟偰偔偩偝偄丅
   *
   * @param argStart  奐巒擭寧擔(YYYYMMDDHHMM乯
   * @param argEnd    廔椆擭寧擔(YYYYMMDDHHMM乯
   * @return 帪娫丄暘偺儕僗僩<BR>
   */
  //***************************************************************************
  public static String[] getTimeCount(String argStart, String argEnd) {
    String returnString[] = new String[2];

    if (argStart.length() != 12 || argEnd.length() != 12) {
      throw new IllegalArgumentException("argStart or argEnd length != 12");
    }

    int intArgStartY = Integer.parseInt(argStart.substring(0, 4));
    int intArgStartM = Integer.parseInt(argStart.substring(4, 6)) - 1;
    int intArgStartD = Integer.parseInt(argStart.substring(6, 8));
    int intArgStartH = Integer.parseInt(argStart.substring(8, 10));
    int intArgStartMin = Integer.parseInt(argStart.substring(10));
    //堷悢偱梌偊傜傟偨奐巒擔晅傪愝掕
    Calendar calendarStart = GregorianCalendar.getInstance();
    calendarStart.set(intArgStartY, intArgStartM,
                      intArgStartD, intArgStartH, intArgStartMin);
    //奐巒擔晅偺儈儕昩庢摼
    long lngStartMilSec = (calendarStart.getTime()).getTime();

    int intArgEndY = Integer.parseInt(argEnd.substring(0, 4));
    int intArgEndM = Integer.parseInt(argEnd.substring(4, 6)) - 1;
    int intArgEndD = Integer.parseInt(argEnd.substring(6, 8));
    int intArgEndH = Integer.parseInt(argEnd.substring(8, 10));
    int intArgEndMin = Integer.parseInt(argEnd.substring(10));
    //堷悢偱梌偊傜傟偨廔椆擔晅傪愝掕
    Calendar calendarEnd   = GregorianCalendar.getInstance();
    calendarEnd.set(intArgEndY, intArgEndM,
                    intArgEndD, intArgEndH, intArgEndMin);
    //廔椆擔晅偺儈儕昩庢摼
    long lngEndMilSec = (calendarEnd.getTime()).getTime();

    //廔椆擔晅偺儈儕昩亅奐巒擔晅偺儈儕昩
    long calcMilsec = lngEndMilSec - lngStartMilSec;
    returnString[0] = String.valueOf(calcMilsec / (1000 * 60 * 60));
    returnString[1] = String.valueOf(calcMilsec % (1000 * 60 * 60) / (1000 * 60));

    return returnString;
  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨擭偑塠擭偐暯擭偐傪僠僃僢僋偟傑偡丅
   *
   * @param argYear  擭(YYYY乯
   * @return    true:塠擭丄false:暯擭
   */
  //***************************************************************************
  public static boolean isLeapYear(String argYear) {
    if (argYear.length() != 4) {
    throw new IllegalArgumentException("argYear length != 4");
    }

    int intArgY = Integer.parseInt(argYear);
    //堷悢偱梌偊傜傟偨擭傪愝掕
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(Calendar.YEAR, intArgY);
    GregorianCalendar gcalendar = new GregorianCalendar();
    if (gcalendar.isLeapYear(calendar.get(Calendar.YEAR))) {
      return true;
    } else {
      return false;
    }
  }

  //***************************************************************************
  /**
   * 巜掕偝傟偨惗偑塠擭偐暯擭偐傪僠僃僢僋偟傑偡丅
   *
   * @param argYear  擭(YYYY乯
   * @return    true:塠擭丄false:暯擭
   */
  //***************************************************************************
  public static boolean isLeapYear(int argYear) {
    String strYear = Integer.toString(argYear);
    return isLeapYear(strYear);
  }

  //***************************************************************************
  /**
   * 擭寧擔偺彂幃曄姺傪峴偄傑偡丅
   * 擔晅偺懨摉惈僠僃僢僋偼峴偄傑偣傫丅僠僃僢僋屻偺擔晅傪僙僢僩偟偰偔偩偝偄丅
   *
   * @param     argDay 惣楋擭寧擔(YYYYMMDD宍幃)
   * @param     argType 曄姺彂幃<br>
   *            DateUtility.YYYYMMDD<br>
   *            DateUtility.YYYYMM<br>
   *            DateUtility.YYYY<br>
   *            DateUtility.YYMMDD<br>
   *            DateUtility.YYMM<br>
   *            DateUtility.YY<br>
   *            DateUtility.MMDD<br>
   *            DateUtility.MM<br>
   *            DateUtility.DD
   * @param     argSeparator 僙僷儗乕僞<br>
   *            DateUtility.SLASH   YYYY/MM/DD<br>
   *            DateUtility.JAPANESE  YYYY擭MM寧DD擔<br>
   *            DateUtility.NONE    僙僷儗乕僞側偟
   * @return    彂幃曄姺偝傟偨暥帤楍
   */
  //***************************************************************************
  public static String formatDate(String argDay, int argType, int argSeparator) {
    String result = null;

    switch (argSeparator) {
      case DateUtility.SLASH:
        result = formatDate(argDay, argType, "/", "/");
        break;
      case DateUtility.JAPANESE:
        result = formatDate(argDay, argType, "擭", "寧");
        result = addString(result, argType, "擭", "寧", "擔");
        break;
      case DateUtility.NONE:
        result = formatDate(argDay, argType, "", "");
        break;
      default:
        throw new IllegalArgumentException("argSeparator is error");
    }

    return result;

  }

  //***************************************************************************
  /**
   * 擭寧擔偺彂幃曄姺傪峴偄傑偡丅
   * 擔晅偺懨摉惈僠僃僢僋偼峴偄傑偣傫丅僠僃僢僋屻偺擔晅傪僙僢僩偟偰偔偩偝偄丅
   *
   * @param     argDay 惣楋擭寧擔(YYYYMMDD宍幃)
   * @param     argType 曄姺彂幃<br>
   *            DateUtility.YYYYMMDD<br>
   *            DateUtility.YYYYMM<br>
   *            DateUtility.YYYY<br>
   *            DateUtility.YYMMDD<br>
   *            DateUtility.YYMM<br>
   *            DateUtility.YY<br>
   *            DateUtility.MMDD<br>
   *            DateUtility.MM<br>
   *            DateUtility.DD
   * @param     argS1 擭寧偺僙僷儗乕僞
   * @param     argS2 寧擔偺僙僷儗乕僞
   * @return    彂幃曄姺偝傟偨暥帤楍
   */
  //***************************************************************************
  public static String formatDate(String argDay, int argType, String argS1, String argS2) {
    String result = null;

    if (argDay.length() != 8) {
      throw new IllegalArgumentException("argDate length != 8");
    }

    switch (argType) {
      case DateUtility.YYYYMMDD:
        result = argDay.substring(0, 4) + argS1 + argDay.substring(4, 6) + argS2 + argDay.substring(6, 8);
        break;
      case DateUtility.YYYYMM:
        result = argDay.substring(0, 4) + argS1 + argDay.substring(4, 6);
        break;
      case DateUtility.YYYY:
        result = argDay.substring(0, 4);
        break;
      case DateUtility.YYMMDD:
        result = argDay.substring(2, 4) + argS1 + argDay.substring(4, 6) + argS2 + argDay.substring(6, 8);
        break;
      case DateUtility.YYMM:
        result = argDay.substring(2, 4) + argS1 + argDay.substring(4, 6);
        break;
      case DateUtility.YY:
        result = argDay.substring(2, 4);
        break;
      case DateUtility.MMDD:
        result = argDay.substring(4, 6) + argS2 + argDay.substring(6, 8);
        break;
      case DateUtility.MM:
        result = argDay.substring(4, 6);
        break;
      case DateUtility.DD:
        result = argDay.substring(6, 8);
        break;
      default:
        throw new IllegalArgumentException("argType is error");
    }

    return result;
  }

  //***************************************************************************
  /**
   * String宆(YYYYMMDD)傪Date宆偵曄姺偟傑偡丅
   * @param argYMD 擭寧擔(YYYYMMDD)
   * @return 巜掕偝傟偨擭寧擔偺Date宆
   */
  //***************************************************************************
  public static Date parseDate(String argYMD) {
    //僷儔儊乕僞僠僃僢僋
    if (argYMD == null || argYMD.length() != 8) {
      throw new IllegalArgumentException("argument is wrong");
    }

    //Date宆傊曄姺
    String year = argYMD.substring(0, 4);
    String month = argYMD.substring(4, 6);
    String date = argYMD.substring(6);
    return parseDate(year, month, date);
  }

  //***************************************************************************
  /**
   * 僨儕儈僞晅偒偺String宆(YYYY/MM/DD)傪Date宆偵曄姺偟傑偡丅
   * @param argYMD 擭寧擔(YYYY/MM/DD)
   * @param argDelimiter 僨儕儈僞
   * @return 巜掕偝傟偨擭寧擔偺Date宆
   */
  //***************************************************************************
  public static Date parseDate(String argYMD, String argDelimiter) {
    //僷儔儊乕僞僠僃僢僋
    if (argYMD == null || argYMD.length() != 10 || argDelimiter == null) {
      throw new IllegalArgumentException("argument is wrong");
    }

    int index[] = new int[2];
    index[0] = argYMD.indexOf(argDelimiter, 0);
    index[1] = argYMD.indexOf(argDelimiter, index[0] + 1);
    if (index[0] == -1 || index[1] == -1) {
      throw new IllegalArgumentException("argument is wrong");
    }

    //Date宆傊曄姺
    String year = argYMD.substring(0, index[0]);
    String month = argYMD.substring(index[0] + 1, index[1]);
    String date = argYMD.substring(index[1] + 1);
    return parseDate(year, month, date);
  }

  //***************************************************************************
  /**
   * 擭丒寧丒擔偺String宆傪Date宆偵曄姺偟傑偡丅
   * @param argYear 擭(4寘)
   * @param argMonth 寧(2寘)
   * @param argDate 擔(2寘)
   * @return 巜掕偝傟偨擭寧擔偺Date宆
   */
  //***************************************************************************
  public static Date parseDate(String argYear, String argMonth, String argDate) {
    //僷儔儊乕僞僠僃僢僋
    if (argYear == null || argMonth == null || argDate == null ||
        argYear.length() != 4 || argMonth.length() != 2 ||
        argDate.length() != 2) {
      throw new IllegalArgumentException("argument is wrong");
    }

    //Date宆傊曄姺
    Date date = createDate(Integer.parseInt(argYear),
                           Integer.parseInt(argMonth),
                           Integer.parseInt(argDate));
    return date;
  }

  //***************************************************************************
  /**
   * Date宆傪String宆(YYYYMMDD)偵曄姺偟傑偡丅
   * @param argYMD  擔晅宆
   * @return String宆(YYYYMMDD)
   */
  //***************************************************************************
  public static String parseString(Date argYMD) {
    //僷儔儊乕僞僠僃僢僋
    if (argYMD == null) {
      throw new IllegalArgumentException("argument is wrong");
    }

    //String宆傊曄姺
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    return formatter.format(argYMD);
  }

  //***************************************************************************
  /**
   * Date宆傪String宆(YYYY/MM/DD)偵曄姺偟傑偡丅
   * @param argYMD  擔晅宆
   * @param argDelimiter 僨儕儈僞
   * @return String宆(YYYY/MM/DD)
   */
  //***************************************************************************
  public static String parseString(Date argYMD, String argDelimiter) {
    //僷儔儊乕僞僠僃僢僋
    if (argYMD == null || argDelimiter == null) {
      throw new IllegalArgumentException("argument is wrong");
    }

    //String宆傊曄姺
    String format = "yyyy" + argDelimiter + "MM" + argDelimiter + "dd";
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    return formatter.format(argYMD);
  }

  //***************************************************************************
  /**
   * Date宆傪擭偺傒偺String宆(YYYY)偵曄姺偟傑偡丅
   * @param argYMD  擔晅宆
   * @return 擭偺傒偺String宆(YYYY)
   */
  //***************************************************************************
  public static String getYear(Date argYMD) {
    //僷儔儊乕僞僠僃僢僋
    if (argYMD == null) {
      throw new IllegalArgumentException("argument is wrong");
    }

    //String宆傊曄姺
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
    return formatter.format(argYMD);
  }

  //***************************************************************************
  /**
   * Date宆傪寧偺傒偺String宆(MM)偵曄姺偟傑偡丅
   * @param argYMD  擔晅宆
   * @return 寧偺傒偺String宆(MM)
   */
  //***************************************************************************
  public static String getMonth(Date argYMD) {
    //僷儔儊乕僞僠僃僢僋
    if (argYMD == null) {
      throw new IllegalArgumentException("argument is wrong");
    }

    //String宆傊曄姺
    SimpleDateFormat formatter = new SimpleDateFormat("MM");
    return formatter.format(argYMD);
  }

  //***************************************************************************
  /**
   * Date宆傪擔晅偺傒偺String宆(DD)偵曄姺偟傑偡丅
   * @param argYMD  擔晅宆
   * @return 擔晅偺傒偺String宆(DD)
   */
  //***************************************************************************
  public static String getDate(Date argYMD) {
    //僷儔儊乕僞僠僃僢僋
    if (argYMD == null) {
      throw new IllegalArgumentException("argument is wrong");
    }

    //String宆傊曄姺
    SimpleDateFormat formatter = new SimpleDateFormat("dd");
    return formatter.format(argYMD);
  }

  //***************************************************************************
  //private儊僜僢僪
  //***************************************************************************

  //***************************************************************************
  /**
   * 寧傗擔傪惍悢宆偐傜暥帤宆偵曄姺偟傑偡丅<br>
   * 侾寘偺悢抣偺応崌偼丄慜偵'0'傪晅梌偟傑偡丅
   *
   * @param     argValue  惍悢宆偺悢抣
   * @return    暥帤宆偺悢抣
   */
  //***************************************************************************
 private static String toCharacter(int argValue) {
    String value = Integer.toString(argValue);
    if (argValue < 10) {
      return "0" + value;
    } else {
      return value;
    }
  }

  //***************************************************************************
  /**
   * DateUtility.JAPANESE偺彂幃曄姺偺応崌偵暥帤楍偺嵟屻偵暥帤傪捛壛偟傑偡丅
   *
   * @param     argString 曄姺屻擔晅暥帤楍
   * @param     argType 曄姺彂幃<br>
   *            DateUtility.YYYYMMDD<br>
   *            DateUtility.YYYYMM<br>
   *            DateUtility.YYYY<br>
   *            DateUtility.YYMMDD<br>
   *            DateUtility.YYMM<br>
   *            DateUtility.YY<br>
   *            DateUtility.MMDD<br>
   *            DateUtility.MM<br>
   *            DateUtility.DD
   * @param     argYear 擭暥帤
   * @param     argMonth 寧暥帤
   * @param     argDate 擔暥帤
   * @return    彂幃曄姺偝傟偨暥帤楍
   */
  //***************************************************************************
  private static String addString(String argString, int argType, String argYear, String argMonth, String argDate) {
    String result = null;

    switch (argType) {
      case DateUtility.YYYYMMDD:
        result = argString + argDate;
        break;
      case DateUtility.YYYYMM:
        result = argString + argMonth;
        break;
      case DateUtility.YYYY:
        result = argString + argYear;
        break;
      case DateUtility.YYMMDD:
        result = argString + argDate;
        break;
      case DateUtility.YYMM:
        result = argString + argMonth;
        break;
      case DateUtility.YY:
        result = argString + argYear;
        break;
      case DateUtility.MMDD:
        result = argString + argDate;
        break;
      case DateUtility.MM:
        result = argString + argMonth;
        break;
      case DateUtility.DD:
        result = argString + argDate;
        break;
      default:
        throw new IllegalArgumentException("argType is error");
    }

    return result;
  }

  //***************************************************************************
  /**
   * Date宆傪嶌惉偟傑偡丅
   * @param argYear 擭
   * @param argMonth 寧
   * @param argDate 擔
   * @return 巜掕偝傟偨擭寧擔偺擔晅宆
   */
  //***************************************************************************
  private static Date createDate(int argYear, int argMonth, int argDate) {
    GregorianCalendar gregorianCalendar =
        new GregorianCalendar(argYear, argMonth - 1, argDate);
    gregorianCalendar.setLenient(false);
    return gregorianCalendar.getTime();
  }
}

posted on 2007-04-10 17:54 天外飞仙 阅读(521) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: