[英]How to fetch Records for a week
I am using Java 7 and fetching records for a week.For valid_from column I am subtracting -7
from current date below. 我正在使用Java 7并获取记录一周。 对于valid_from列,我从下面的当前日期中减去
-7
。 The format of date in DB is 12-FEB-18
. DB中的日期格式为
12-FEB-18
。 For valid_to column I am using sysdate. 对于valid_to列,我正在使用sysdate。 I am not getting correct valid_from date.
我没有得到正确的有效日期。 Can anyone review this what is wrong here.
任何人都可以在这里查看这是怎么回事。
Format formatter = new SimpleDateFormat("dd-MMM-yy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -7);
Date todate1 = cal.getTime();
Date startdate = ((DateFormat)formatter).parse(formatter.format(todate1));
System.out.println(todayWithZeroTime);
String sql_qry = "SELECT a.ACCOUNT_ID from tableName a where a.STATUS_TYPE_KEY='ACTIVE' "
+ "and a.VALID_FROM >"
+ startdate+ " and a.VALID_TO > sysdate";
How can I parse only Date here. 我如何只在这里解析日期。 Currently I am getting
Tue Feb 13 00:00:00 GMT 2018
. 目前我将在
Tue Feb 13 00:00:00 GMT 2018
收到消息。 I want 13-FEB-18
which I can send as variable in where condition. 我想要
13-FEB-18
,可以在where条件中将其作为变量发送。
Please suggest 请建议
You are converting a Date
to a String
then back to a Date
. 您正在将
Date
转换为String
然后转换回Date
。
Then you are using this Date
object in your query, so it's toString()
method gets called and yields a String
representation which is probably not the one you wanted. 然后,您在查询中使用此
Date
对象,因此将调用toString()
方法并产生String
表示形式,该表示形式可能不是您想要的。
Avoid the last conversion from String
to Date
and just use 避免最后一次从
String
转换为Date
并仅使用
String startdate = formatter.format(todate1);
Note that you also have to escape the date string with quotes : 请注意,您还必须使用引号对日期字符串进行转义:
String sql_qry = "SELECT a.ACCOUNT_ID from tableName a where "
+ "a.STATUS_TYPE_KEY='ACTIVE' "
+ "and a.VALID_FROM > '"
+ startdate+ "' and a.VALID_TO > sysdate";
Also consider having a look at Java time API and at How to use PreparedStatement 还可以考虑看看Java时间API和如何使用PreparedStatement
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.