⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dateclass.java

📁 一个关于日期处理的基础类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * @return  開始日期大於結束日期返回負數、小於為正數、相等為0
   */
  public static long compareToDay( String b_date, String e_date )
  {
      return DateClass.getLongDate( e_date ) - DateClass.getLongDate(b_date);
  }
  /**
   * 返回距离某日期(字符串)一定时间的日期(字符串)
   * @param todayStr 参数日期
   * @param years 距离的年份数(可带正负号)
   * @param months 距离的月份数(可带正负号)
   * @param days 距离的天数(可带正负号)
   * @return 相应的日期字符串
   */
  public static String getCorrespondingDate(String todayStr, int years,
                                            int months, int days) {
    String correspondingDate = "";
    if (todayStr == null || todayStr.trim().length() != 10) {
      System.out.print("param error in getCorrespondingDate()");
      return "";
    }
    int[] splitDate = DateClass.splitDateStr2int(todayStr);
    if (splitDate == null || splitDate.length != 3) {
      System.out.print("日期参数错误,无法计算相应日期");
      return "";
    }
    try {
      GregorianCalendar retDate = new GregorianCalendar( (splitDate[0] + years),
          (splitDate[1] - 1 + months), (splitDate[2] + days));
      Date d = retDate.getTime();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      correspondingDate = sdf.format(d);
      return correspondingDate;
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return "";
    }
  }

  /**
   * 将格式为yyyy-mm-dd的日期字符串拆分成int数组(以年、月、日的顺序存放)
   * @param dateStr 格式为yyyy-mm-dd的日期字符串
   * @return 拆分后的int数组
   */
  public static int[] splitDateStr2int(String dateStr) {
    if (dateStr == null || dateStr.trim().length() <= 0) {
      return null;
    }
    int[] splitdate = new int[3];
    int yyyy, mm, dd = 0;
    String year = dateStr.substring(0, 4);
    String month = dateStr.substring(5, 7);
    String day = dateStr.substring(8, 10);
    try {
      yyyy = Integer.parseInt(year);
      mm = Integer.parseInt(month);
      dd = Integer.parseInt(day);
    }
    catch (Exception e) {
      e.printStackTrace();
      return null;
    }
    splitdate[0] = yyyy;
    splitdate[1] = mm;
    splitdate[2] = dd;
    return splitdate;
  }

  /**
   * 根据输入的年、月,返回该月的月末日期(日期格式为yyyy-mm-dd)
   * @param year 年份
   * @param month 月份
   * @return 该月的月末日期字符串
   */
  public static String getMonthEndDay(String year, String month) {
    if (year == null || year.trim().length() != 4 || month == null ||
        month.trim().length() <= 0) {
      System.out.print("param error in getMonthEndDay()");
      return "";
    }
    String endDay = "";
    int yyyy, mm = 0;
    try {
      yyyy = Integer.parseInt(year);
      mm = Integer.parseInt(month);
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return "";
    }
    try {
      GregorianCalendar retDate = new GregorianCalendar(yyyy,
          mm, 0);
      Date d = retDate.getTime();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      endDay = sdf.format(d);
      return endDay;
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return "";
    }
  }

  /**
   * 根据传入的日期,返回传入格式的日期
   * @param date  传入的日期
   * @param fmt  传入格式
   * @return 转换后的日期
   */
  public static String getfmtdate( String date, String fmt )
  {
  	 String str="";
  	 String ft[] = new String[3];
  	 String str2="";
  	 String str3[] = new String[3];;

  	 if( null == date ) return "";
     if( date.trim().length()==10 )
     {
         str3 = null;
         str2 = date.substring( 4, 5 );
         str3 = date.split( str2 );
     }
     else if( date.trim().length() == 8 )
     {
         str3[0] = date.substring(0,4);
         str3[1] = date.substring(4,6);
         str3[2] = date.substring(6,8);
     }
     else if( date.trim().length() == 6 )
     {
         str3 = new String[2];
         str3[0] = date.substring(0,4);
         str3[1] = date.substring(4,6);
     }
     else return "";
  	 if( null == fmt || ( fmt.length() != 10 && fmt.length() != 11 && fmt.length() != 8) )
  	 {
  	 	ft[0] = "-";
  	 	ft[1] = "-";
  	 	ft[2] = "";
  	 }
  	 else if( fmt.length() == 10 )
  	 {
  	 	ft[0] = fmt.substring( 4,5 );
  	 	ft[1] = fmt.substring( 7,8 );
  	 	ft[2] = "";
  	 }
     else if( fmt.length() == 8 )
     {
         if( !fmt.toUpperCase().endsWith( "D" ) )
         {
             ft[0] = fmt.substring( 4, 5 );
             ft[1] = fmt.substring( 7, 8 );
             ft[2] = "";
         }
         else
         {
             ft[0] = "";
             ft[1] = "";
             ft[2] = "";
         }
     }
  	 else if( fmt.length() == 11 )
  	 {
  	 	ft[0] = fmt.substring( 4,5 );
  	 	ft[1] = fmt.substring( 7,8 );
  	 	ft[2] = fmt.substring( 10 );
  	 }

  	 for( int i=0; i< str3.length; i++ )
  	 {
  	 	str = str + str3[i] + ft[i];
  	 }
  	 return str;
  }

  /**
   * 根据输入的年、季,返回该季度最后一天的日期(日期格式为yyyy-mm-dd)
   * @param year 年份
   * @param season 季度
   * @return 该季度最后一天的日期
   */
  public static String getSeasonEndDay(String year, String season) {
    if (year == null || year.trim().length() != 4 || season == null ||
        season.trim().length() <= 0) {
      System.out.print("param error in getSeasonEndDay()");
      return "";
    }
    String seasonEndDay = "";
    if (season.equalsIgnoreCase("1")) {
      seasonEndDay = year.trim() + "-03-31";
    }
    else if (season.equalsIgnoreCase("2")) {
      seasonEndDay = year.trim() + "-06-30";
    }
    else if (season.equalsIgnoreCase("3")) {
      seasonEndDay = year.trim() + "-09-30";
    }
    else if (season.equalsIgnoreCase("4")) {
      seasonEndDay = year.trim() + "-12-31";
    }
    else {
      System.out.print("param error in getSeasonEndDay()");
      return "";
    }
    return seasonEndDay;
  }

  /**
   * 根据输入的年、季,返回该季第一天的日期(日期格式为yyyy-mm-dd)
   * @param year 年份
   * @param season 季度
   * @return 该季度第一天的日期
   */
  public static String getSeasonBegDay(String year, String season) {
    if (year == null || year.trim().length() != 4 || season == null ||
        season.trim().length() <= 0) {
      System.out.print("param error in getSeasonEndDay()");
      return "";
    }
    String seasonBegDay = "";
    if (season.equalsIgnoreCase("1")) {
      seasonBegDay = year.trim() + "-01-01";
    }
    else if (season.equalsIgnoreCase("2")) {
      seasonBegDay = year.trim() + "-04-01";
    }
    else if (season.equalsIgnoreCase("3")) {
      seasonBegDay = year.trim() + "-07-01";
    }
    else if (season.equalsIgnoreCase("4")) {
      seasonBegDay = year.trim() + "-10-01";
    }
    else {
      System.out.print("param error in getSeasonEndDay()");
      return "";
    }
    return seasonBegDay;
  }


  /**
   * 根据日期字符串返回所属的季度值
   * @param dateStr 日期字符串
   * @return 该日期所属的季度值
   */
  public static String getSeason(String dateStr) {
    if (dateStr == null || dateStr.trim().length() != 10) {
      System.out.println("param error in getSeason");
      return "";
    }
    String season = "";
    String month = dateStr.trim().substring(5, 7);
    if (month.equals("01") || month.equals("02") || month.equals("03")) {
      season = "1";
    }
    else if (month.equals("04") || month.equals("05") || month.equals("06")) {
      season = "2";
    }
    else if (month.equals("07") || month.equals("08") || month.equals("09")) {
      season = "3";
    }
    else if (month.equals("10") || month.equals("11") || month.equals("12")) {
      season = "4";
    }
    else {
      System.out.println("param error in getSeason");
      return "";
    }
    return season;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -