select * from zdyjkzbjl where jsbh='330400111' and TO_CHAR( pbrq , 'yyyy-mm-dd')='2023-05-23' and bc ='1' and TO_CHAR( jbsj , 'yyyy-mm-dd hh24:MI')<>'2023-05-23 07:50' and TO_CHAR( jiaobsj , 'yyyy-mm-dd hh24:MI')<>'2023-05-23 19:50' and TO_CHAR( now() , 'yyyy-mm-dd hh24:MI:SS') BETWEEN '2023-05-23 07:50:00' and '2023-05-23 17:00:00'; --一班次 23/07- 23/17 提醒时间 select * from zdyjkzbjl where jsbh='330400111' and TO_CHAR( pbrq , 'yyyy-mm-dd')='2023-05-23' and bc ='2' and TO_CHAR( jbsj , 'yyyy-mm-dd hh24:MI')='2023-05-23 19:50' and TO_CHAR( jiaobsj , 'yyyy-mm-dd hh24:MI')='2023-05-24 01:50' and TO_CHAR( now(), 'yyyy-mm-dd hh24:MI:SS') BETWEEN '2023-05-23 17:00:00' and '2023-05-24 01:30:00'; --二班次 23/17 -24/01 提醒时间 select * from zdyjkzbjl where jsbh='330400111' and TO_CHAR( pbrq , 'yyyy-mm-dd')='2023-05-23' and bc='3' and TO_CHAR( jbsj , 'yyyy-mm-dd hh24:MI')<>'2023-05-24 01:50' and TO_CHAR( jiaobsj , 'yyyy-mm-dd hh24:MI')<>'2023-05-24 07:50' and TO_CHAR( now() , 'yyyy-mm-dd hh24:MI:SS') BETWEEN '2023-05-24 01:30:00' and '2023-05-24 07:50:00'; 如何根据时间来分别调用其中的一个sql ,使用java 和mybatis sql 里面的日期根据当天日期传入
时间: 2023-08-12 09:07:42 浏览: 184
可以通过Java的Date类和SimpleDateFormat类来获取当前日期和时间,并将其转化为需要的格式。
示例代码如下:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class SqlQuery {
public static void main(String[] args) {
String jsbh = "330400111";
String bc = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String nowStr = sdf2.format(now);
String dateStr = sdf.format(now);
int hour = now.getHours();
if (hour >= 7 && hour < 17) {
bc = "1";
String sql = "select * from zdyjkzbjl where jsbh='" + jsbh + "' and TO_CHAR( pbrq , 'yyyy-mm-dd')='" + dateStr + "' and bc ='1' and TO_CHAR( jbsj , 'yyyy-mm-dd hh24:MI')<>'" + dateStr + " 07:50' and TO_CHAR( jiaobsj , 'yyyy-mm-dd hh24:MI')<>'" + dateStr + " 19:50' and TO_CHAR( now() , 'yyyy-mm-dd hh24:MI:SS') BETWEEN '" + dateStr + " 07:50:00' and '" + dateStr + " 17:00:00'";
System.out.println(sql);
} else if (hour >= 17 && hour < 24) {
bc = "2";
String sql = "select * from zdyjkzbjl where jsbh='" + jsbh + "' and TO_CHAR( pbrq , 'yyyy-mm-dd')='" + dateStr + "' and bc ='2' and TO_CHAR( jbsj , 'yyyy-mm-dd hh24:MI')='" + dateStr + " 19:50' and TO_CHAR( jiaobsj , 'yyyy-mm-dd hh24:MI')='" + sdf.format(new Date(now.getTime() + 24 * 60 * 60 * 1000)) + " 01:50' and TO_CHAR( now(), 'yyyy-mm-dd hh24:MI:SS') BETWEEN '" + dateStr + " 17:00:00' and '" + sdf.format(new Date(now.getTime() + 24 * 60 * 60 * 1000)) + " 01:30:00'";
System.out.println(sql);
} else {
bc = "3";
String sql = "select * from zdyjkzbjl where jsbh='" + jsbh + "' and TO_CHAR( pbrq , 'yyyy-mm-dd')='" + sdf.format(new Date(now.getTime() + 24 * 60 * 60 * 1000)) + "' and bc='3' and TO_CHAR( jbsj , 'yyyy-mm-dd hh24:MI')<>'" + sdf.format(new Date(now.getTime() + 24 * 60 * 60 * 1000)) + " 01:50' and TO_CHAR( jiaobsj , 'yyyy-mm-dd hh24:MI')<>'" + sdf.format(new Date(now.getTime() + 24 * 60 * 60 * 1000)) + " 07:50' and TO_CHAR( now() , 'yyyy-mm-dd hh24:MI:SS') BETWEEN '" + sdf.format(new Date(now.getTime() + 24 * 60 * 60 * 1000)) + " 01:30:00' and '" + sdf.format(new Date(now.getTime() + 24 * 60 * 60 * 1000)) + " 07:50:00'";
System.out.println(sql);
}
}
}
```
在上述代码中,我们首先获取当前时间并转化为所需格式,然后根据小时数判断应该调用哪个SQL语句,最后拼接出完整的SQL语句并打印出来。
阅读全文