Date1.Compare(Date2)就可以进行比较了,不管是java.util.Date java.sql.Dateto kaukiyou(小全) : 象这样,就无法判断大小 to stefli() : 楼主要不带Time的日期比较
可以转化为两个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();
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); } }
SimpleDateFormat smdf = new SimpleDateFormat("yyyy-MM-dd");
String Strdate= new String(smdf.format(date));
Date的compareTo有两种compareTo(Object o)跟compareTo(Date d)
注意型别
你那面的那个dtCurrent不知道是什么类型
觉得你写的也没有错啊,是不是没有parse过,能看看你的两个日期怎么得到的码。getDate得到的是一个月中的第几天。
一个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
使用如下代码取当前日期:
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();
}
然后再进行日期比较
直接使用compareTo是可以的
getDate().getTime() > dtCurrent.getTime()
象这样,就无法判断大小
to stefli() :
楼主要不带Time的日期比较
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);
}
}
象这样,就无法判断大小
请指教:年月日是数字,为什么不能比较大小?
20060910 == 20060910 20060911 > 20060910
Date d2 = new Date();long l1 = d1.getTime();
long l2 = d2.getTime();if (l1 < l2)
{
...
}
new Date(System.currentTimeMillis())楼主很可能用的是java.util.Date类型,精度到微妙,默认时分秒为此时刻,自然比较前者小于后者