JAVA实现日期+整数转换为日期的类

<pre class="java" name="code">import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 该类主要完成对字符的验证和过滤
 * @author
 *
 */
public class StringUtil {

//转换日期格式
      public  static  java.sql.Date   parseDate(String   s)throws   Exception{   
          String   []   str=s.split("-");   
          if(str.length!=3){   
          throw   new   Exception("日期格式不正确!");   
          }   
            
          int   year=Integer.parseInt(str[0]);   
          int   month=Integer.parseInt(str[1]);   
          int   day=Integer.parseInt(str[2]);   
            
          if(year>9999   ||   year<0){   
          throw   new   Exception("日期格式不正确!");   
          }   
          if(month<1   ||   month>12){   
          throw   new   Exception("日期格式不正确!");   
          }   
          if(day<1   ||   day>31){   
          throw   new   Exception("日期格式不正确!");   
          }   
            
          return   new   java.sql.Date(year-1900,month-1,day);   
          }
      /**
       * 把字符串解析为Date对象
       * @param str 日期字符串
       * @return 日期对象
       * @throws ParseException
       */
      public static java.util.Date StrToDate(String str) throws ParseException {
          //指定日期格式
           SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
           java.util.Date date = null;
           //把字符串解析为Date对象
            date = format.parse(str);
        
           return date;
        }
      /**
       * 根据mar是几月返回一个日期类型对象
       * @param date 日期对象
       * @return 字符串
       */
      public static String DateToStr(java.util.Date date) {   
        //指定日期格式
           SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
           //把日期对象解析为指定格式的字符串
           String str = format.format(date);   
           return str;   
        }
      /**
       * 日期+整数=日期  (分钟数)
       * @param dt 日期
       * @param count  整数
       * @return 日期
       */
      public static java.sql.Date MINUTE_Shift(java.util.Date dt, int count)
      {
              Calendar cal = Calendar.getInstance();
              //使用给定的 Date 设置此 Calendar 的时间
              cal.setTime(dt);
              //根据日历的规则,为给定的日历字段添加或减去指定的时间量
              //Calendar.MINUTE,用来设置 整数与日期中的 什么相加 Calendar.MINUTE为钟
              cal.add(Calendar.MINUTE, count);
              return new java.sql.Date(cal.getTimeInMillis());
//java.sql.Date(cal.getTimeInMillis()).toString()  // 默认格式化日期转义形式 yyyy-mm-dd 的日期
      }

/*  这个能看出效果
public static java.util.Date MINUTE_Shift(java.util.Date dt, int count)
      {
              Calendar cal = Calendar.getInstance();
              //使用给定的 Date 设置此 Calendar 的时间
              cal.setTime(dt);
              //根据日历的规则,为给定的日历字段添加或减去指定的时间量
              //Calendar.MINUTE,用来设置 整数与日期中的 什么相加 Calendar.MINUTE为钟
              cal.add(Calendar.MINUTE, count);
              return new java.util.Date (cal.getTimeInMillis());
      }
*/




}
</pre>
&nbsp;

posted @ 2012-08-03 13:40  sky7034  阅读(800)  评论(0编辑  收藏  举报
蘑菇街女装 货运专家