您的位置:首页 > 编程语言 > Java开发

sql按时间条件查询及java.util.Date计算

2019-02-14 17:14 176 查看

查询最新数据

SELECT * from table
where field=#{field} And collect_time ORDER BY collect_time DESC LIMIT 1

查询十五分钟

select * from table
where field=#{field} And collect_time >= now()-interval 15 minute;

查询今日

SELECT * from table
where field=#{field} And TO_DAYS(now())-TO_DAYS(collect_time)<1 ORDER BY collect_time

查询一周内数据

SELECT * from table
where field=#{field} And TO_DAYS(now())-TO_DAYS(collect_time)<7 ORDER BY collect_time

java.util.Date计算

java.util.Date与long类型转换

Date date=new Date();
date.getTime()

要注意的是计算两个java.util.Date类型数据相差多少秒,是相应的long类型相减后的千分之一,因为long类型相减出来的是毫秒。

public static void main(String[] args) throws InterruptedException {
Date date=new Date();
thread thread=new thread();
thread.start();
sleep(3000);
thread.exit=true;
thread.join();
Date date1=new Date();
System.out.print(date1.getTime()-date.getTime());
}
static class thread extends Thread{
public volatile boolean exit = false;
@Override
public void run() {
while(!exit);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐