我想知道今天和2010.5.1的时间差(用天计算)
我写的代码如下:
                   Date start=new Date(2010,5,1);
                   Date now=new Date();
Long shijian=now.getTime()-start.getTime();
Integer day=(int)(shijian/(24*3600*1000));

String str=day.toString();//把天数改成String类型可是有问题啊,高手帮我解决一下~

解决方案 »

  1.   

    同一年
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();c1.set(2010,4,1);

    System.out.println(c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR));System.out.println((c2.getTime().getTime() - c1.getTime().getTime())/(24 * 3600 *1000));
      

  2.   

    查一下API,可以知道Date这个类有许多个构造方法,其中Date()和Date(int year, int month, int date)是其中的两个。但这两个构造方法除了形式上不同外,还有很大的区别,看看API就知道啦:
    public Date()
      Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecondpublic Date(int year,int month, int date). 
    Allocates a Date object and initializes it so that it represents midnight, local time, at the beginning of the day specified by the year, month, and date arguments. Parameters:
    year - the year minus 1900.   //这里是重点,是你出错的原因所在。要先将year减1900。 
    month - the month between 0-11.
    date - the day of the month between 1-31.
     Date start=new Date(110,5,1);  //将2010减1900
     Date now=new Date();
     Long shijian=now.getTime()-start.getTime();
     Integer day=(int)(shijian/(24*3600*1000));
     String str=day.toString();//把天数改成String类型
      

  3.   

    Date 的构造方法除了Date(),Date(long date)外,都过时了。
      

  4.   

    2楼为什么System.out.println((c2.getTime().getTime() - c1.getTime().getTime())/(24 * 3600 *1000));
    中c2.getTime().getTime()的第二个getTime是什么意思 啊?
      

  5.   

    回楼上,第一个getTime是从calendar类获得date类型,第二个getTime是从date类型得到到1970年1月1日的毫秒数
      

  6.   

    用Calendar吧,Date很多方法都过时了。
      

  7.   

    year - the year minus 1900. //这里是重点,是你出错的原因所在。要先将year减1900。 
    month - the month between 0-11.
    date - the day of the month between 1-31.
      

  8.   

    因为c2.getTime()得到是Date类型的,所以再调用getTime()就是Long类型的毫秒数了哦~呵呵,看api知道的