有两个字符串如20050310和20050223,求它们的相差多少天?

解决方案 »

  1.   

    看了一些论坛上的人,说先求出每个时间距离1970年,也就是有计算机语言开始的时候,然后两个相减,得出的值除以1000*60*60*24,但好像结果还是不对阿。公式是这样的:
    Date date1=new Date(2005,3,10);
    Date date2=new Date(2005,2,23);
    double days=Math.floor((date1.getTime()-date2.getTime())/(1000*60*60*24));
    请各位大侠帮忙看看,谢谢了!
      

  2.   

    public class Test
    {
        public int returnDate(String date,String anotherDate)
        {
            int returnValue=0;
            SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
           
            try
    {
    returnValue=(int)((format.parse(anotherDate).getTime()-format.parse(date).getTime())/(1000*60*60*24));
    }
    catch(ParseException e)
    {
    e.printStackTrace();
    }
            if(returnValue<0)
                returnValue=-returnValue;
            return returnValue;
            
            
        }
        public static void main(String[] args)
        {
           Test t=new Test();
           System.out.println(t.returnDate("20050310","20050311"));
        }
    }
      

  3.   

    好,谢谢classjava(原始野人)。看来csdn有的是牛人啊!
      

  4.   

    强~~~~~~~对了,我是新手,我想请教个问题,我们初学JAVA的菜鸟经常不知道应该调用JDK中的哪个类的哪个方法,请问怎么才能解决目前这种困境