//------------------------------------------------------------------------------
    //
    /**
     * 检查日期是否合法
     * @param _date  需要检查的日期
     * @return 如果日期合法则为true,否则为false
     */
    public synchronized static boolean checkDate(String _date){
        if (_date == null) {
            return false;
        }
        if (_date.length() < 8)
            return false;
        String yy = _date.substring(0, 4);
        String mm = _date.substring(4, 6);
        String dd = _date.substring(6, 8);        try {
            int iyy = Integer.parseInt(yy);
            int imm = Integer.parseInt(mm);
            int idd = Integer.parseInt(dd);            if (iyy > 2050 || iyy < 1950){
                return false;
            }
            if (imm < 1 || imm > 12) return false;
            int iMaxMonthDay = Integer.parseInt(getMaxMonthDay(yy, mm));
            if (idd < 1 || idd > iMaxMonthDay) return false;
            return true;
        }
        catch (Exception e){
            System.err.println(e.getMessage());
            return false;
        }
    }    /**
     * 检查日期是否合法
     * @param _date  需要检查的日期
     * @return 如果日期合法则为true,否则为false
     */
    public synchronized static boolean checkDateTime(String _date){
        if (_date == null) {
            return false;
        }
        if (_date.length() < 14)
            return false;
        String yy = _date.substring(0, 4);
        String mm = _date.substring(4, 6);
        String dd = _date.substring(6, 8);
        String ho = _date.substring(8, 10);
        String mi = _date.substring(10, 12);
        String se = _date.substring(12, 14);        try {
            int iyy = Integer.parseInt(yy);
            int imm = Integer.parseInt(mm);
            int idd = Integer.parseInt(dd);            if (iyy > 2050 || iyy < 1950){
                return false;
            }
            if (imm < 1 || imm > 12) return false;
            int iMaxMonthDay = Integer.parseInt(getMaxMonthDay(yy, mm));
            if (idd < 1 || idd > iMaxMonthDay) return false;
            int iho = Integer.parseInt(ho);
            int imi = Integer.parseInt(mi);
            int ise = Integer.parseInt(se);            if (iho < 0 || iho > 24) return false;
            if (imi < 0 || imi > 60) return false;
            if (ise < 0 || ise > 60) return false;
            return true;
        }
        catch (Exception e){
            System.err.println(e.getMessage());
            return false;
        }
    }    /**
     * 检查时间是否合法
     * @param _ho 小时
     * @param _mi 分钟
     * @param _se 秒钟
     * @return 如果合法则为true,否则为false
     */
    public static synchronized boolean checkTime(String _ho, String _mi, String _se){
        try {
            int iho = Integer.parseInt(_ho);
            int imi = Integer.parseInt(_mi);
            int ise = Integer.parseInt(_se);            if (iho < 0 || iho > 24) return false;
            if (imi < 0 || imi > 60) return false;
            if (ise < 0 || ise > 60) return false;
            return true;
        }
        catch (Exception e){
            System.err.println(e.getMessage());
            return false;
        }
    }
//------------------------------------------------------------------------------
    //
    /**
     * 获得某月的最大的天数
     * @param yy 年
     * @param mm 月
     * @return 该月的最大天数
     */
    public static synchronized String getMaxMonthDay(String yy,String mm){        Calendar cal = Calendar.getInstance();
        try{
            int y = Integer.parseInt(yy);
            int m = Integer.parseInt(mm);
            cal.set(y, m-1, 1);
        }
        catch(Exception e){
            System.err.println( "  DataFunction Exception " + e.getMessage());
        }
        String maxDay = String.valueOf(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return maxDay;
    }    /**
     * 获得某月的最大天数
     * @param _yyyymm 年月
     * @return 该月的最大天数
     */
    public static synchronized String getMaxMonthDay(String _yyyymm){
        String yy = _yyyymm.substring(0, 4);
        String mm = _yyyymm.substring(4, 6);
        return getMaxMonthDay(yy, mm);
    }
//------------------------------------------------------------------------------
    //
    /**
     * 得到过去的某个月
     * @param _mm 向前推算的月数
     * @return 该月的日期
     */
    public DateFormat getPreviousMonth(int _mm){
        if (_mm < 0)
            return getNextMonth(-_mm);
        int iyy = Integer.parseInt(year);
        int imm = Integer.parseInt(month);
        while (imm - _mm <= 0) {
            _mm -= 12;
            iyy--;
        }
        imm -= _mm;        String yy = String.valueOf(iyy);
        String mm = String.valueOf(imm);        yy = StringFunction.updateZero(yy, 4);
        mm = StringFunction.updateZero(mm, 2);        DateFormat df = new DateFormat(yy + mm + "01");
        return df;
    }    /**
     * 得到将来的某个月
     * @param _mm 向后推算的月数
     * @return 该月的日期
     */
    public DateFormat getNextMonth(int _mm){
        if (_mm < 0)
            return getPreviousMonth(-_mm);
        int iyy = Integer.parseInt(year);
        int imm = Integer.parseInt(month);
        while (_mm + imm > 12) {
            _mm -= 12;
            iyy++;
        }
        imm += _mm;        String yy = String.valueOf(iyy);
        String mm = String.valueOf(imm);        yy = StringFunction.updateZero(yy, 4);
        mm = StringFunction.updateZero(mm, 2);        DateFormat df = new DateFormat(yy + mm + "01");
        return df;
    }    /**
     * 得到上个月
     * @return 该月的日期
     */
    public DateFormat getPreviousMonth(){
        return getPreviousMonth(1);
    }    /**
     * 得到下个月
     * @return 该月的日期
     */
    public DateFormat getNextMonth(){
        return getNextMonth(1);
    }

解决方案 »

  1.   

    //------------------------------------------------------------------------------
        //
        /**
         * 得到过去的某日
         * @param _day 向前推算的天数
         * @return 该日的日期
         */
        public DateFormat getPreviousDay(int _day){
            if (_day < 0)
                return getNextDay(-_day);
            int iyy = Integer.parseInt(year);
            int imm = Integer.parseInt(month);
            int idd = Integer.parseInt(day);
            DateFormat df = new DateFormat();
            df.setDate(iyy, imm, idd);        if (idd - _day > 0){
                idd -= _day;
                df.setDate(iyy, imm, idd);
            }
            else {
                _day -= idd;
                df = df.getPreviousMonth();
                df.setDate(df.getMonthDate() + getMaxMonthDay(df.getMonthDate()));
                df = df.getPreviousDay(_day);
            }
            return df;
        }    /**
         * 得到将来的某日
         * @param _day 向后推算的天数
         * @return 该日的日期
         */
        public DateFormat getNextDay(int _day){
            if (_day < 0)
                return getPreviousDay(-_day);
            int iyy = Integer.parseInt(year);
            int imm = Integer.parseInt(month);
            int idd = Integer.parseInt(day);
            DateFormat df = new DateFormat();
            df.setDate(iyy, imm, idd);        if (idd + _day <= Integer.parseInt(df.getMaxMonthDay(df.getMonthDate()))){
                idd += _day;
                df.setDate(iyy, imm, idd);
            }
            else {
                _day -= Integer.parseInt(df.getMaxMonthDay(df.getMonthDate())) - idd + 1;
                df = df.getNextMonth();
                df.setDate(df.getMonthDate() + "01");
                df = df.getNextDay(_day);
            }
            return df;
        }
    //------------------------------------------------------------------------------
        //
        /**
         * 获得某日的过去几个月的日期
         * @param _date 指定的日期
         * @param _month 向前推算的月数
         * @return 结果的日期
         */
        public synchronized static DateFormat getPreviousMonth(String _date, int _month){
            if (!DateFormat.checkDate(_date)) {
                throw new NullPointerException("日期不正确");
            }
            return new DateFormat(_date).getPreviousMonth(_month);
        }    /**
         * 获得某日的将来几个月的日期
         * @param _date 指定的日期
         * @param _month 向后推算的日期
         * @return 结果的日期
         */
        public synchronized static DateFormat getNextMonth(String _date, int _month){
            if (!DateFormat.checkDate(_date)) {
                throw new NullPointerException("日期不正确");
            }
            return new DateFormat(_date).getNextMonth(_month);
        }    /**
         * 获得某日的过去若干天的日期
         * @param _date 指定的日期
         * @param _day 向前推算的日期
         * @return 结果的日期
         */
        public synchronized static DateFormat getPreviousDay(String _date, int _day){
            if (!DateFormat.checkDate(_date)) {
                throw new NullPointerException("日期不正确");
            }
            return new DateFormat(_date).getPreviousDay(_day);
        }    /**
         * 获得某日的将来若干天的日期
         * @param _date 指定的日期
         * @param _day 向后推算的日期
         * @return 结果的日期
         */
        public synchronized static DateFormat getNextDay(String _date, int _day){
            return new DateFormat(_date).getNextDay(_day);
        }
    //------------------------------------------------------------------------------
        //
        /**
         * 比较两个日期的前后
         * @param _date1 日期1
         * @param _date2 日期2
         * @return 比较的结果,
         * 如果>0,则_date1>_date2;=0,两个日期相等;<0,则_date1<_date2
         */
        public synchronized static int compare(String _date1, String _date2){
            if (!checkDate(_date1)) {
                throw new NullPointerException("日期不正确");
            }
            if (!checkDate(_date2)) {
                throw new NullPointerException("日期不正确");
            }
            int nDate1 = Integer.parseInt(_date1);
            int nDate2 = Integer.parseInt(_date2);
            return nDate1 - nDate2;
        }
    //------------------------------------------------------------------------------
        public String toString(){
            if (getHour() == null || getMinute() == null || getSecond() == null) {
                return getDate();
            }
            return getDate() + getTime();
        }
    //------------------------------------------------------------------------------
        public boolean equals(Object o){
            if (o instanceof DateFormat) {
                return toString().equals(o.toString());
            }
            else {
                DateFormat obj = new DateFormat(o.toString(), true);
                return equals(obj);
            }
        }
    //------------------------------------------------------------------------------
    /**
     * 比较大小
     * @param o
     * @return
     */
        public int compareTo(Object o){
            if (o instanceof DateFormat) {
                return toString().compareTo(o.toString());
            }
            else {
                DateFormat obj = new DateFormat(o.toString(), true);
                return compareTo(obj);
            }
        }//------------------------------------------------------------------------------
    /**
     * 返回两个日期的距离天数
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return
     */
    public static long getPeriodDayCount(String startDate, String endDate){
    Date d1 = new DateFormat(startDate).toDate();
    Date d2 = new DateFormat(endDate).toDate();
    long periodTime = d2.getTime() - d1.getTime();
    return periodTime/24/60/60/1000;
    }
    //------------------------------------------------------------------------------
        public Date toDate(){
            Calendar c = Calendar.getInstance();
            int iy = Integer.parseInt(getYear());
            c.set(Calendar.YEAR, Integer.parseInt(getYear()));
            c.set(Calendar.MONTH, Integer.parseInt(getMonth()) -1);
            c.set(Calendar.DATE, Integer.parseInt(getDay()));
            c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(getHour()));
            c.set(Calendar.MINUTE, Integer.parseInt(getMinute()));
            c.set(Calendar.SECOND, Integer.parseInt(getSecond()));        return c.getTime();
        }
    //------------------------------------------------------------------------------
        public static void main(String[] args) {
            try {
    System.out.println(DateFormat.getPeriodDayCount("2003-06-01","2003-06-07"));
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
      

  2.   

    需要用到的StringFunction类
    import java.util.*;/**
     * 处理字符串相关功能的类
     * <p>Copyright: 2004</p>
     * @author Filippo Guan
     * @version 1.1
     */
    public class StringFunction {
    private String str; public StringFunction(String _Str) {
    str = _Str;
    } /**
     * 判别两个字符串是否匹配,其中'*'代表任意长的字符串(包括0长字符串),'?'代表单字节字符
     * @param _Str 传入的字串
     * @return 若不匹配,则返回false
     */
    public boolean like(String _Str) {
    /*        if (_Str.length() == 0) {
                return false;
            }
            int i = 0;
            int j = 0;
            while (j < _Str.length()) {
                if (i >= this.str.length()) {
                    return false;
                }
                char ch = _Str.charAt(j);
                if (ch == '*') {
                    do {
                        j++;
                        if (j >= _Str.length()) {
                            return true;
                        }
                    }
                    while (_Str.charAt(j) == '*' ||
                           _Str.charAt(j) == '?');
                    i = this.str.indexOf(_Str.charAt(j), i);
                    if (i == -1) {
                        return false;
                    }
                    else {
                        i++;
                        j++;
                    }
                    continue;
                }
                else if (ch == '?') {
                    i++;
                    j++;
                    continue;
                }
                else if (ch == this.str.charAt(i)) {
                    i++;
                    j++;
                }
                else {
                    return false;
                }
            }
            return true;
    */
    return str.matches(".*" + _Str + ".*");
    } public synchronized static boolean like(String str1, String str2) {
    return new StringFunction(str1).like(str2);
    } //------------------------------------------------------------------------------
    //
    /**
     * 将字符串扩充到长度为num,如果超过长度则去除超过的部分,否则则在前补0
     * @param num 指定扩充到的长度
     * @return 返回的字串
     */
    public String updateZero(int num) {
    return updateChar(num, '0');
    } public synchronized static String updateZero(String str, int num) {
    return new StringFunction(str).updateZero(num);
    } //------------------------------------------------------------------------------
    //
    /**
     * 将字符串扩充到长度为num,如果超过长度则去除超过的部分,
     * 否则则在前面不上某个字符
     * @param num 指定扩充到的长度
     * @param c 补上的字符
     * @return 返回的字串
     */
    public String updateChar(int num, char c) {
    if (str.length() == num) {
    return str;
    }
    else if (str.length() < num) {
    for (int i = str.length(); i < num; i++) {
    str = c + str;
    }
    return str;
    }
    else {
    return str.substring(str.length() - num);
    }
    } //------------------------------------------------------------------------------
    //
    /**
     * 如果字符串不为null,那么就在两面加上"",
     * 该方法一般用于书写Sql语句,其功能可以被SqlExpression中的类似方法取代
     * @param s 传入的字串
     * @return 返回的字串
     */
    public synchronized static String setNull(String s) {
    if (s != null) {
    if (!s.equals("")) {
    s = "'" + s + "'";
    }
    else {
    s = null;
    }
    }
    return s;
    } /**
     * 如果字符串为"",那么就把它设为null。
     * 该方法一般用于书写Sql语句中处理日期时,
     * 其功能已被SqlExpression中的类似方法所取代
     * @param s 传入的字串
     * @return 返回的字串
     */
    public synchronized static String setDateNull(String s) {
    if (s != null) {
    if (s.equals("")) {
    s = null;
    }
    }
    return s;
    } /**
     * 将数字转换为字符串,并在的两边加上引号,
     * 该方法一般用于书写Sql语句,其功能可以被SqlExpression中的类似方法取代
     * @param s 传入的数字
     * @return 返回的字串
     */
    public synchronized static String setNull(long s) {
    return "'" + String.valueOf(s) + "'";
    } /**
     * 将数字转换为字符串,并在的两边加上引号,
     * 该方法一般用于书写Sql语句,其功能可以被SqlExpression中的类似方法取代
     * @param s 传入的数字
     * @return 返回的字串
     */
    public synchronized static String setNull(double s) {
    return "'" + String.valueOf(s) + "'";
    } /**
     * 在字符串的末尾加上换行的符号
     * @param s 传入的字串
     * @return 返回的字串
     */
    public synchronized static String addReturn(String s) {
    return s + "\n";
    } /**
     * 如果字符串为空,则返回一个"&nbsp;"符号
     * @param s 传入的字串
     * @return 返回的字串
     */
    public synchronized static String getHtmlFormat(String s) {
    if (s != null) {
    if (s.trim().equals("")) {
    return "&nbsp;";
    }
    else {
    return s.trim();
    }
    }
    else {
    return "&nbsp;";
    }
    }
      

  3.   

    //------------------------------------------------------------------------------
    //
    /**
     * 如果传入的字符串为null,则返回一个""
     * @param s 传入的字串
     * @return 返回的字串
     */
    public synchronized static String getStringFormat(Object o) {
    if (o == null) {
    return "";
    }
    else {
    String s = o.toString();
    return s.trim();
    }
    } //------------------------------------------------------------------------------
    //
    /**
     * 保证字符串可以转化为数值型,若不能则返回"0"
     * @param s 传入的字串
     * @return 返回的字串
     */
    public synchronized static String getNumberFormat(Object o) {
    if (o == null) {
    return "0";
    }
    String s = o.toString();
    try {
    double dou = Double.parseDouble(s);
    }
    catch (Exception e) {
    return "0";
    }
    return s;
    } public static double getDouble(Object o) {
    return Double.parseDouble(getNumberFormat(o));
    }
    public static long getLong(Object o) {
    return (long) getDouble(o);
    }
    public static int getInt(Object o) {
    return (int) getDouble(o);
    }
    public static float getFloat(Object o) {
    return (float) getDouble(o);
    }
    //------------------------------------------------------------------------------
    //
    /**
     * 把一个字符串转换成字符串YYYYMMDD的形式
     * @param _date 传入的字串
     * @return 返回的字串
     */
    public static synchronized String getDateFormat(String _date) {
    if (_date == null) {
    return "";
    }
    if (_date.length() == 8) {
    return _date;
    }
    if (_date.length() > 8) {
    return _date.substring(0, 4)
    + _date.substring(5, 7)
    + _date.substring(8, 10);
    }
    return "";
    }
    //------------------------------------------------------------------------------
    //
    /**
     * 把一个字符串转换成字符串YYYYMMDDHHMISS的形式
     * @param _date 传入的字串
     * @return 返回的字串
     */
    public static synchronized String getDateTimeFormat(String _date) {
    if (_date == null) {
    return "";
    }
    if (_date.length() == 8) {
    _date += "000000";
    }
    if (_date.length() == 14) {
    return _date;
    }
    if (_date.length() > 14) {
    return _date.substring(0, 4)
    + _date.substring(5, 7)
    + _date.substring(8, 10)
    + _date.substring(11, 13)
    + _date.substring(14, 16)
    + _date.substring(17, 19);
    }
    return "";
    } //------------------------------------------------------------------------------
    //
    /**
     * 获取一个字符串的绝对长度(如果遇到汉字字符则算两个)
     * @param s 传入的字串
     * @return 字串的绝对长度
     */
    public static int absoluteLength(String s) {
    if (s == null) {
    return 0;
    }
    try {
    return new String(s.getBytes("GB2312"), "8859_1").length();
    }
    catch (Exception e) {
    return s.length();
    }
    } //------------------------------------------------------------------------------
    //
    /**
     * 对一个字符串的绝对长度进行拆解(如果遇到汉字字符会把它当作两个字符处理)
     * @param s 传入的字串
     * @param start 起始绝对位置
     * @param end 终止绝对位置
     * @return 返回的字串
     */
    public static String absoluteSubstring(String s, int start, int end) {
    if (s == null) {
    return null;
    }
    try {
    String s2 = new String(s.getBytes("GB2312"), "8859_1");
    s2 = s2.substring(start, end);
    return new String(s2.getBytes("8859_1"), "GB2312");
    }
    catch (Exception e) {
    return s.substring(start, end);
    }
    } /**
     * 在一个字符串两边加上"-"号
     * @param s 传入的字串
     * @param num 加上"-"号的个数
     * @return 返回的字串
     */
    public static String addLine(String s, int num) {
    for (int i = 0; i < num; i++) {
    s = "-" + s + "-";
    }
    return s;
    } /**
     * 扩充一个字串,使其绝对长度为指定的长度,如果过长就截断,过短就补充指定的字串
     * @param str 传入的字串
     * @param updateStr 填充的字串
     * @param num 指定的长度
     * @param flag 填补字符串的位置:true的话在前面填补、false在后面填补
     * @return 返回的字串
     */
    public static String updateAbsoluteLength(
    String str,
    String updateStr,
    int num,
    boolean flag) {
    if (updateStr == null) {
    return str;
    }
    if (str == null) {
    str = "";
    for (int i = 0; i < num; i++) {
    str += updateStr;
    }
    return str;
    }
    if (absoluteLength(str) == num) {
    return str;
    }
    else if (absoluteLength(str) < num) {
    for (int i = absoluteLength(str); i < num; i++) {
    if (flag) {
    str = updateStr + str;
    }
    else {
    str = str + updateStr;
    }
    }
    return str;
    }
    else {
    return absoluteSubstring(str, 0, absoluteLength(str) - num);
    }
    } public static String updateAbsoluteLength(
    String str,
    String updateStr,
    int num) {
    return updateAbsoluteLength(str, updateStr, num, true);
    } //------------------------------------------------------------------------------
    public static void main(String[] args) {
    String c = new String("管振宇");
    String c2 = new String("刘蓓丽");
    String c3 = new String("administrator");
    System.out.println(StringFunction.updateAbsoluteLength(c, "&&nbsp;", 28));
    System.out.println(StringFunction.updateAbsoluteLength(c2, "&&nbsp;", 28));
    System.out.println(StringFunction.updateAbsoluteLength(c3, "&&nbsp;", 28));
    }}
      

  4.   

    还有,csdn太烂了
    害得我要把代码一段段拼回去
    应该支持代码功能,或是附件/链接
    不过,还是要再次谢谢管药师同学!
    ^_^
      

  5.   

    此同学非彼同学
    乃“同”学java之同学也
    好绕口!^_^
      

  6.   

    哦,呵呵!
    那就共同学习java,努力!
      

  7.   

    阴阳历在csdn上有过讨论吧,好像没有一个简洁的算法,我记得当时讨论的结果就是得将近百年的信息保存的程序中,再进行计算,这叫查表法。
      

  8.   

    上次看见的就是js实现!
    zcjl(假球迷:最爱米兰!) 米兰的球迷?
    Filippo.Inzaghi 偶像!
      

  9.   

    马儿帝尼,大师级的人物!
    我现在也不怎么看足球了,没意思!
    现在支持姚明,看NBA
      

  10.   

    ...........
    写得太长了,没耐心看,就看了CheckDate 方法,感觉....
    太滥了
    看我的代码
    public boolean isDate(String strDate, String strFormat) {
    if(strFormat == null || strFormat.length() == 0){
    return false;
    }
    int formatLen = strFormat.length();
    if(strDate == null || strDate.length() != formatLen)
    return false;
    try{
    java.text.SimpleDateFormat simpledateformat = new java.text.SimpleDateFormat(strFormat);
    simpledateformat.setLenient(false);
    simpledateformat.parse(strDate);
    return true;
    }catch(Exception exception){
    return false;
    }
    }
    strDate 是日期,strFormat是要检查的日期的格式
    举例:
    isDate("2004年12月01日","yyyy年MM月dd日")
    isDate("2004/12/01/","yyyy/MM/dd/")
    isDate("2004/12","yyyy/MM")