【发布时间】:2011-10-25 17:51:48
【问题描述】:
我正在尝试将字符串格式的日期转换为 sql 日期并基于该查询数据库来获取结果。
字符串格式的日期为:2011-08-11 09:16:00.0
所以我通过使用方法将它转换为sql日期:
public static convertStringToSqlDate(String dateString){
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date parsedUtilDate = formater.parse(dateString);
java.sql.Date sqlDate= new java.sql.Date(parsedUtilDate.getTime());
return sqlDate;
}
结果日期是:2011-08-11
但是在进行查询时我没有得到想要的输出
完整的代码是
def startDate = params. startDate
def endDate = params. endDate
def formattedTripStartDate =Trip.convertStringToSqlDate(startDate);
def formattedTripEndDate =Trip.convertStringToSqlDte(endDate);
def listOfContracts = Rule.findAll("FROM Rule WHERE name LIKE ? AND client_id = ? AND STR_TO_DATE(contract_begins,'%Y-%m-%d')<= ? AND STR_TO_DATE(contract_terminates,'%Y-%m-%d')>= ?",["%"+q_param+"%",clientId,formattedTripStartDate,formattedTripEndDate] )
我哪里错了?
在数据库中,contract_begins 存储为:2011-08-23 00:00:00
合同域类是
class Contract extends Rule {
Date contractBegins
Date contractTerminates
int runningDays
Double contractValue
Double estimatedRevenue
Double actualRevenue
static constraints = {
contractBegins(nullable:true)
contractTerminates(nullable:true)
runningDays(nullable:true)
contractValue(nullable:true)
estimatedRevenue(nullable:true)
actualRevenue(nullable:true)
}
}
【问题讨论】:
-
“Sting”是一位上了年纪的摇滚明星。您的意思可能是“字符串”...
-
为什么要将日期作为字符串存储在数据库中
-
正如 Jigar Joshi 所说,您应该将日期作为日期存储在数据库中
-
@Hussy 那你为什么要这样做:“STR_TO_DATE”。顺便说一句,没有足够的信息来猜测哪里出了问题。显示数据库中的数据,解释每个字段的数据类型。
-
好的,我将更新代码...我将添加合同域类
标签: java mysql grails date groovy