我想得到两个日期之间相差的天数 数据库是Mysql

解决方案 »

  1.   

    MySQL中的日期是字符串,你需要先把两个日期转换成Date类型,然后再分别getTime();得到long数字,两数字相减,再换算成天就可以了。import java.util.*;public class Test {  public Test(){
        String [] str = "2005-06-06".split("-"); //第一日期
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR , Integer.parseInt(str[0]));
        c.set(Calendar.MONTH, Integer.parseInt(str[1]) - 1);
        c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(str[2]));
        Date date_1 = c.getTime();
        str = "2005-06-01".split("-"); //第二日期
        c.set(Calendar.YEAR , Integer.parseInt(str[0]));
        c.set(Calendar.MONTH, Integer.parseInt(str[1]) - 1);
        c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(str[2]));
        Date date_2 = c.getTime();
        System.out.println((date_1.getTime() - date_2.getTime()) / 24 / 60 / 60 / 1000);
      }
      public static void main(String[] args) {
        Test test = new Test();
      }}
      

  2.   

    SELECT TO_DAYS("2005-06-04") - TO_DAYS("2005-02-04")
      

  3.   

    我也是这样做的但强制转换成int的时候有时会少了一天 
      

  4.   

    我不是从数据库中取值出来比较 之所以写数据库是Mysql 是因为其他数据库有工具可以进行这种数据比较
      

  5.   

    if (m_VaildateTextField.getText().trim().length() > 0) {
            Date date = new Date();
            Date setdate = new Date();
            String title = m_VaildateTextField.getText();//值为2006-6-10
            setdate = (new SimpleDateFormat("yyyy-MM-dd")).parse(title);
           double  aa=((setdate.getTime() - date.getTime()) /(24 * 60 * 60 * 1000));
           int ddd =  (int)((setdate.getTime() - date.getTime()) /(24 * 60 * 60 * 1000));
            System.out.println(aa);
            System.out.println(ddd);
           if((aa-ddd) >0.0){
             ddd=ddd+1;
           }
           System.out.println(ddd);得到的值为364
            m_VaildatedayTextField.setText(String.valueOf(ddd));
          }