2008-9-27 13:05:00 与 2008-9-27 12:05:00 & 2008-9-27 13:45:00 比较?java 如何实现相差?

解决方案 »

  1.   

    JDK里有个时间比较函数....
      

  2.   

    Calendar.before(Object when) 
    判断此 Calendar 表示的时间是否在指定 Object 表示的时间之前,返回判断结果。
    Calendar.after(Object when) 
    判断此 Calendar 表示的时间是否在指定 Object 表示的时间之后,返回判断结果。
      

  3.   

    看看这个是不是你要的结果:
              SimpleDateFormat format=new SimpleDateFormat("yyyy-mm-dd H:mm:ss");
      Date date=format.parse("2008-9-27 13:05:00"); 
      Date date1=format.parse("2008-9-27 14:05:00");
      long begin=date.getTime();
      long end=date1.getTime();
      long time=(begin>end)?begin-end:end-begin;
      System.out.println(time);//打印的结果是毫秒;
      

  4.   

    支持3楼
    Calendar.before(Object when) 
    判断此 Calendar 表示的时间是否在指定 Object 表示的时间之前,返回判断结果。 
    Calendar.after(Object when) 
    判断此 Calendar 表示的时间是否在指定 Object 表示的时间之后,返回判断结果。
      

  5.   

    Date d = new GregorianCalendar(2008, 9, 27, 13, 05).getTime();
    Calendar now = Calendar.getInstance();
    Calendar t1 = Calendar.getInstance();
    t1.setTime(d);
            
    System.out.println( now.compareTo(t1) );我想准确知道相差多少..
      

  6.   


    public static void main(String[] args) throws Exception {
    String DATE_FORMAT = "yyyy-MM-dd hh:mm";
    Date d = new Date();
    SimpleDateFormat s = new SimpleDateFormat(DATE_FORMAT);
    Date time1 = s.parse("2008-6-1 10:10");
    Date time2 = s.parse("2008-8-1 10:10");
    System.out.println((time2.getTime() - time1.getTime()) / (1000 * 3600 * 24)); }