都转换成日期类型进行比较 不知道你用什么数据库? oracle如下select * from a where to_date(BEGINDATE,'yyyy-mm-dd hh24:mi')>to_date('2010-08-12 08:20','yyyy-mm-dd hh24:mi) and to_date(BEGINDATE,'yyyy-mm-dd hh24:mi')<to_date('2010-11-12 08:20','yyyy-mm-dd hh24:mi);
不用转化,直接比较字符串就可以 select * from a where startTime >= '2010-08-12 08:20' and endTime <= '2010-09-12 08:20'
select * from table where date between date1 and date2;
Date d1=sdf.parse("2010-08-12 08:20");
Date d1=sdf.parse("2010-09-12 08:20");
d1.compareTo(d2)
可是我要查数据库,如何进行呢?
数据库中的字段为字符串类型,
查询条件中输入的也是字符串类型,
我如何对其进行sql查询呢
例如 字段 "BEGINDATE"的值为"2010-09-12 08:20"
字段 "AFTERDATE"的值为"2010-10-12 08:20"
通过输入查询条件
将大于:"2010-08-12 08:20"并且小于"2010-11-12 08:20"的日期查询出来
不知道你用什么数据库?
oracle如下select * from a where to_date(BEGINDATE,'yyyy-mm-dd hh24:mi')>to_date('2010-08-12 08:20','yyyy-mm-dd hh24:mi) and to_date(BEGINDATE,'yyyy-mm-dd hh24:mi')<to_date('2010-11-12 08:20','yyyy-mm-dd hh24:mi);
select * from a where startTime >= '2010-08-12 08:20' and endTime <= '2010-09-12 08:20'
如果是ORACLE数据库,这是可以的
public int getBetweenMils(String dateA, String dateB) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
java.util.Date d1 = df.parse(dateA);
java.util.Date d2 = df.parse(dateB);
dayNumber = (d2.getTime() - d1.getTime())
} catch (Exception e) {
e.printStackTrace();
}
return (int) milsNumber;
}
获取相差的月份