我想將字符串格式的日期轉換爲sql日期並基於該查詢數據庫來獲得結果。 日期字符串格式爲:2011-08-11 09:16:00.0
所以我使用的方法將其轉換爲SQL DATE:字符串日期轉換問題
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)
}
}
「斯汀」是一個老搖滾明星。你可能是指「字符串」 ... –
爲什麼至今存儲爲字符串在DB –
作爲Jigar Joshi說,你應該存儲日期爲你的數據庫 – njzk2