两个变量,都是datatime的,打印出来一个是Wed Apr 05 00:00:00 CST 2006 ,一个是 2006-04-05这样比较if(getDate().compareTo(dtCurrent) >= 0)结果却是:Wed Apr 05 00:00:00 CST 2006 < 2006-04-05请教,怎样比较这两个变量的日期?不比时间。

解决方案 »

  1.   


            SimpleDateFormat  smdf   =   new   SimpleDateFormat("yyyy-MM-dd"); 
            String Strdate= new String(smdf.format(date));
      

  2.   

    compareTo可以的,
    Date的compareTo有两种compareTo(Object o)跟compareTo(Date d)
    注意型别
    你那面的那个dtCurrent不知道是什么类型
      

  3.   

    比较年月日?
    觉得你写的也没有错啊,是不是没有parse过,能看看你的两个日期怎么得到的码。getDate得到的是一个月中的第几天。
      

  4.   

    l楼上各位,两个变量“EffectiveDate”和“dtCurrent”都是datetime类型,但格式不同,
    一个Wed Apr 05 00:00:00 CST 2006 ,一个是 2006-04-05
    其中“EffectivetDate”由客户端传来,“dtCurrent”是系统当前时间,dtCurrent=new Date(System.currentTimeMillis());我希望是:
    if(EffectiveStartDate>=dtCurrent)
    {
    System.out.println("effective date >= current date ");

    else
    {
    System.out.println("effective date < current date ");
    }Wed Apr 05 00:00:00 CST 2006 应该等于 2006-04-05
      

  5.   

    要实现你的目的,取当前日期不能使用简单使用dtCurrent=new Date(System.currentTimeMillis());
    使用如下代码取当前日期:
    Calendar now = Calendar.getInstance();
    SimpleDateFormat yearAndMonth = new SimpleDateFormat("yyyy-MM-dd");
    Date dtCurrent =null;
    try {
        dtCurrent = yearAndMonth.parse( yearAndMonth.format(now.getTime()));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    然后再进行日期比较
      

  6.   

    楼主我怀疑你那个客户端传过来的是String
      

  7.   

    Error(573,73): incompatible types; found: java.util.Date, required: java.sql.Date这不是时间的两种类型吗?怎么转换?
      

  8.   

    既然都是Date类型
    直接使用compareTo是可以的
      

  9.   

    java.sql.Date是继承java.util.Date的,建议看看java API.这种东西网上资料很多的
      

  10.   

    我同意laughsmile(海边的星空)的说法
      

  11.   

    用时间的毫秒数进行比较就可以了阿。
    getDate().getTime() > dtCurrent.getTime()
      

  12.   

    ^_^,还是getDate().getTime()比较简单。
      

  13.   

    Date1.Compare(Date2)就可以进行比较了,不管是java.util.Date  java.sql.Dateto kaukiyou(小全) :
    象这样,就无法判断大小
    to stefli() :
    楼主要不带Time的日期比较
      

  14.   

    可以转化为两个String值得比较
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class Test
    {
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static void compare_Date(Date EffectiveStartDate){
    Date dtCurrent=new Date();
            
    String EffectiveStart_Date = sdf.format(EffectiveStartDate);
            String dtCurrent_Date = sdf.format(dtCurrent);
            
         if(EffectiveStart_Date.compareTo(dtCurrent_Date) >= 0)
         {
            System.out.println("effective date >= current date");
         } 
         else
         {
            System.out.println("effective date < current date");
         }
    }

        public static void main(String[] args) {
             
          Date EffectiveStartDate = new Date();
             compare_Date(EffectiveStartDate);
        }
    }
      

  15.   

    to kaukiyou(小全) :
    象这样,就无法判断大小
    请指教:年月日是数字,为什么不能比较大小?
    20060910 == 20060910 20060911 > 20060910
      

  16.   

    我觉得用long型来比较最好的。方法是:把时间类型(不管是什么类型的,Date也行、Calendar也行)转换成long型,然后比较两个long的大小。Date d1 = new Date();
    Date d2 = new Date();long l1 = d1.getTime();
    long l2 = d2.getTime();if (l1 < l2)
    {
        ...
    }
      

  17.   

    getDate()是从jdbc提供的方法,取出来的是java.sql.Date,精度到天,默认时分秒均为0;
    new Date(System.currentTimeMillis())楼主很可能用的是java.util.Date类型,精度到微妙,默认时分秒为此时刻,自然比较前者小于后者