public static Long compare(){
long d1,d2,d3;
Date startDate=new Date(1899,12,31);
d1=date.getTime();
System.out.println(d1);
d2=startDate.getTime();
System.out.println(d2);
d3=d1-d2;
d3/=1000*60*60*24;
Long d4 = new Long(d3);
return d4;
}
代码如上,处理时间到2000年1月份都没有问题,到了2月份就不对了,明显的差了几天

解决方案 »

  1.   

    date是怎么进去的, 是什么值?
      

  2.   

    import java.util.Date;public class test {
    public static void main(String[] args){

    Date d = new Date(1899,12,31);

    System.out.println(d.getYear());
    System.out.println(d.getMonth());
    System.out.println(d.getDay());
    System.out.println(d.toLocaleString());
    }
    }D:\>java test
    1900
    0
    5
    3800-1-31 0:00:00
      

  3.   

    ---------------------------------------------------------------------------------------
    Date
    public Date(int year,
                int month,
                int day)
    Deprecated. instead use the constructor Date(long date) Constructs a Date object initialized with the given year, month, and day. 
    The result is undefined if a given argument is out of bounds. 
    Parameters:
    year - the year minus 1900; must be 0 to 8099. (Note that 8099 is 9999 minus 1900.)
    month - 0 to 11
    day - 1 to 31
    ---------------------------------------------------------------------------所以你的 year 应该设成 year-1900 ,month = month -1 ,dayjust like this:public static Long test(int year,int month,int day){
    long d1,d2,d3;
    Date startDate= new Date(year-1900,month-1,day);
    d1=date.getTime();
    System.out.println(d1);
    d2=startDate.getTime();
    System.out.println(d2);
    d3=d1-d2;
    d3/=1000*60*60*24;
    Long d4 = new Long(d3);
    return d4;
    }
      

  4.   

    //试试这个,加上了判断输入日期格式是否正确的代码
    public static Long daysAfter12_31_1899(String mmddyyyy) throws Exception {
      java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMddyyyy");  java.util.Date toDate = sdf.parse(mmddyyyy);
      if (!sdf.format(toDate).equals(mmddyyyy))
        throw new Exception("Wrong Date:"+mmddyyyy
                           +". Should be:"+sdf.format(toDate));  return new Long((toDate.getTime()-sdf.parse("12311899").getTime())
                       /(1000*60*60*24));
    }