2011-08-12 6 views
1

문자열 형식의 날짜를 sql 날짜로 변환하고 결과를 얻기 위해 해당 쿼리 데이터베이스를 기반으로 변환하려고합니다. 문자열 형식의 날짜 : 2011-08-11 09:16:00.0 그래서 방법을 사용하여 SQL 날짜로 변환 오전 : 나는 쿼리를 수행 2011-08-11문자열에서 날짜로 변환 할 때의 문제

하지만 동시에 :

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; 
} 

결과 날짜입니다 원하는 출력 못하고

전체 코드는

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 0시 0분 0초

계약 도메인 클래스는 그 자체로 포맷하지 않은

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) 
} 
    } 
+2

"스팅"는 노화 록 스타 :

또한 쿼리를라는 이름의 도메인 클래스에 기준을 추가하여 코드도 친절하게 고려한다. 당신은 아마 "문자열"을 의미합니다 ... –

+3

DB에 –

+1

으로 문자열을 저장하는 이유 Jigar Joshi가 말한대로 날짜를 DB에 날짜로 저장해야합니다. – njzk2

답변

0

날짜 개체입니다, 그것은 단지 날짜를 반환 시간 값. SimpleDateFormat, DateFormat 등과 같은 문자열 형식의 서식 지원 클래스 만 서식을 지정할 수 있습니다.

0

왜 조건이 아닌 findAll 쿼리를 사용 하시겠습니까? 이 같은 것을해야합니다 :

def startDate = params.startDate 
def endDate = params.endDate 
def tripStartDate=Trip.convertStringToSqlDate(startDate); 
def eripEndDate=Trip.convertStringToSqlDte(endDate); 

def c = Contract.createCriteria() 
def results = c.list { 
    like("name", "%${q_param}%") 
    eq("client_id", clientId) 
    ge('contract_begins', tripStartDate) 
    le('contract_terminates','tripEndDate) 
} 

이것은 훨씬 더 깨끗하고 SQL이 어떻게 생겼는지 걱정할 필요가 없습니다!

http://grails.org/doc/latest/ref/Domain%20Classes/createCriteria.htmlhttp://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.4.2에 대해 자세히 살펴보고 더 많은 정보를 얻을 수있는 기준. http://grails.org/doc/latest/ref/Domain%20Classes/namedQueries.html

+0

계약 도메인 클래스를 추가합니다. – Hussy

+0

groovy.lang.MissingMethodException : 메서드의 서명이 없습니다. com.springpeople.steer.trips.TripController.after()는 인수에 적용 가능합니다. 타입 : (java.lang.String, java.sql.Date) values ​​: [contract_begins, 2011-08-12] – Hussy

+0

코드를 수정했습니다. 작동한다면 시도하지 않았지만, 가능할 수도 있습니다 :-) – sbglasius

관련 문제