如题
例如一部影片的下线时间为2013-08-02
            今天的日期为2013-05-09
请问如何算出距离下线还有多少天呢?   
例:最终显示出距离下线还有30天。  就这种效果

解决方案 »

  1.   

    /**说明,由于没有对日期的合法性作判断,故请输入正确的日期,
       且ksn,ksy,ksr是表示日期在前面的那个日期,若放反过来
       输入便会出错。此代码纯粹只是为了输出间隔的天数,故许多
       判断条件并未写出*/
    public class JianGe { public static void main(String[] args) {
    //在前面的年月日
    int ksn=2013;
        int ksy=5;
        int ksr=9;
        //在后面的年月日
        int zzn=2013;
        int zzy=8;
        int zzr=2;
        int ts=0;
        
        ts = buTongNian(ksn,ksy,ksr,zzn,zzy,zzr);
        System.out.println("间隔天数为:" + ts); }

    //返回这一年这个月的天数
    public static int tianShu(int nian, int yue) {
      if(nian == 0 || yue > 12 || yue < 1) {
        System.out.println("您传入的数据非法");
        return -1;
      }
      
      if(yue == 2) {
        if(shiFouRunNian(nian)) {
         return 29;
        } else {
          return 28;
        }
      }
      
      int ts = 0;
      
      switch(yue) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
          ts = 31;
          break;
        case 4:
        case 6:
        case 9:
        case 11:
          ts = 30;
          break;
        default:
          
      }
      
      return ts;
      
    }

    //判断是否为闰年
    private static boolean shiFouRunNian(int nian) {
        boolean run = false;
      
        if(((nian % 4 == 0) && (nian % 100 != 0)) || (nian % 400 == 0)) {
            run = true;
      }
      
        return run;
    }
    //同年
    public static int tongNian(int nian, int ksy, int ksr, int zzy, int zzr) {
    int yueShu = 0;
    for(int j = ksy; j < zzy; j++) {
    yueShu += tianShu(nian , j);

    }
    int ts = yueShu - ksr + zzr;
    return ts;
    }

    // 不同年
    public static int buTongNian(int ksn,int ksy,int ksr,int zzn,int zzy,int zzr) {

    int ts0=0;   //开始年与终止年之间的年份的天数和,不包括终止年
    int ts1=0;   //开始年所在的那一年中的天数
    int ts2=0;   //终止年所在的这一年中的天数


    ts1 = tongNian(ksn , 1 , 1 , ksy , ksr) ;
    ts2 = tongNian(zzn , 1 , 1 , zzy , zzr) ;
    for(int i = ksn ; i < zzn ; i++) {
    if(shiFouRunNian(i) == true) 
        ts0 += 366;
     else ts0 += 365;
    }
     int ts =Math.abs( ts0 - ts1 +ts2 );
     return ts;

    } }
      

  2.   

    楼主参考下这里
    Java 计算两个日期相差的天数
    http://www.verydemo.com/demo_c128_i13320.html
      

  3.   


    package com.zyc.test;import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;/**
     * 计算两个日期相差的天数
     *
     */
    public class Test04 { public static void main(String[] args) {
    long days = getDays("2013-05-09","2013-08-02");
    System.out.println("距离下线还有" + days + "天");
    } static long getDays(String now,String deadline) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    try {
    Date d1 = (Date) format.parse(now);
    Date d2 = (Date) format.parse(deadline);
    long day = d2.getTime() - d1.getTime();
    return day / 1000 / 60 / 60 / 24;
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return -1;
    }
    }