数据库的有字符串转换为时间戳的函数unix_timestamp(?)。(以下只是个例子,如果是找某时间段内的数据,直接通过>、<比较字符串或者用between and即可)
public int countNum(String startTime, String endTime)
{
String SQL = "SELECT left(create_time, 10) day_date, COUNT(1) cnt FROM train where create_time>=unix_timestamp(?) and create_time<=unix_timestamp(?) AND create_status = 2 GROUP BY day_date desc limit 100";
int count = new Long((long) autoProcessDao.queryForMap(SQL, new Object[] { startTime, endTime, platform, payType }, null).get("count_divide")).intValue();
return count;
}
而其他的日期类型转换如下:
时间转字符串
select date_format(now(), '%Y-%m-%d');
结果:2016-01-05
时间转时间戳
select unix_timestamp(now());
结果:1452001082
字符串转时间
select str_to_date('2016-01-02', '%Y-%m-%d %H');
结果:2016-01-02 00:00:00
字符串转时间戳
select unix_timestamp('2016-01-02');
结果:1451664000
时间戳转时间
select from_unixtime(1451997924);
结果:2016-01-05 20:45:24
时间戳转字符串
select from_unixtime(1451997924,'%Y-%d');
结果:2016-01-05 20:45:24