转换成Calendar类,用Calendar能比较和计算

解决方案 »

  1.   

    public java.sql.Date  StringtoDate(String year,String mon,String day) throws OurException
         { 
     java.sql.Date sdate=null;       int temp;
          //字符串有效性检验,未给出月日的,按1月1日计
           if(year!=null&&year.length()!=0)
           {  //月日赋值
              if(mon==null||mon.length()==0)
              mon="1";
              if(day==null||day.length()==0)
              day="1";
              //其他合法性检验          //2月29日30日31日,平年输出为3月1日;
            if (mon.equals("2")) {
               if ( ( (Integer.parseInt(year)%4 == 0) && (Integer.parseInt(year) % 100 != 0)) ||
                   ( Integer.parseInt(year)%400 == 0)) {
                 if (Integer.parseInt(day) > 29) {
                     day = "1"; //闰年29天
                     mon="3";
                 }
               }
               else {
                 if (Integer.parseInt(day) > 28) {
                   day = "1"; //平年2月份28天
                   mon="3";
                 }
               }        }
              //4、6、9、11月31日,输出为下月1日
            else
            { if((mon.equals("4")||mon.equals("6")||mon.equals("9")||mon.equals("11"))&&(day.equals("31")))
              {day="1";
               temp=Integer.parseInt(mon)+1;
               //temp=(Integer)temp0;
               mon=Integer.toString(temp);
              }
            }
              //给出时间格式
                SimpleDateFormat format1 =new SimpleDateFormat("dd-MM-yyyy");
                String dateStr = day+"-"+mon+"-"+year;
               try
                 {java.util.Date  tempdate=format1.parse(dateStr);
                  sdate=new java.sql.Date(tempdate.getTime());
                  }
                  catch(Exception e) {
                  throw new OurException(e, "300","转换日期时出现异常!");
                  }
           }
             return sdate;
        }
      

  2.   

    int n = str1.compareTo(str2);
    if (n > 0) {}
    else if (n < 0) {}