import java.util.Date;
import java.util.Calendar;
public class TestDateDemo{
public static void main(String args[ ]){
  Date today = new Date();
  System.out.println("Today's date is :"+today);
  Calendar c = Calendar.getInstance();
  c.set(1997,2,3);
  Calendar d = c;
  System.out.println("The initial time is :"+c.get(Calendar.YEAR)+"."+c.get(Calendar.MONTH)+"."+c.get(Calendar.DATE));
  c.add(Calendar.DATE,1);
  c.add(Calendar.HOUR_OF_DAY,1);
  System.out.println("The new date is :"+c.get(Calendar.YEAR)+"."+c.get(Calendar.MONTH)+"."+c.get(Calendar.DATE));
  long days = (c.getTime() - d.getTime())/(1000*60*60*24);
  System.out.println("Two dates differ by "+days+"days");
}
}

解决方案 »

  1.   

    以下是Calendar类的getTime()方法的源代码,可以看到使用getTime()方法时,返回的是一个Date类对象,而你想用Calendar类对象获取毫秒值应该使用getTimeInMillis()方法.    /**
         * Returns a <code>Date</code> object representing this
         * <code>Calendar</code>'s time value (millisecond offset from the <a
         * href="#Epoch">Epoch</a>").
         *
         * @return a <code>Date</code> representing the time value.
         * @see #setTime(Date)
         * @see #getTimeInMillis()
         */
        public final Date getTime() {
            return new Date(getTimeInMillis());
        }