2014-04-01 38 views
1

我Grails中映射到域類的傳統的Oracle表中所列Grails的評價:Oracle日期字段不使用java.Date

class myDomain implements Serializable { 

    String id 
    String baseCountry 
    Date expireDate 

static constraints = { 
} 
static mapping = { 
    version false 
    autoTimestamp false 
    datasource 'OraDataSource' 
    table "MY_ACCOUNT" 
    id column: "LICENSE_NUMBER", generator: "assigned" 
    baseCountry column: "BASE_COUNTRY" 
    expireDate column: "EXPIRE_DATE" NOTE: This is an Oracle DATE type 
} 

設置我的Grails的日期,如下圖所示:

def now = new Date() 
def currentYear = now[YEAR] 
def expireDateBegin = new Date() 
def expireDateEnd = new Date() 

expireDateBegin[YEAR] = currentYear 
expireDateBegin[MONTH] = DECEMBER 
expireDateBegin[DATE] = 30 

expireDateEnd[YEAR] = currentYear +1 
expireDateEnd[MONTH] = JANUARY 
expireDateEnd[DATE] = 01   

當我使用動態取景器帶有日期條件不返回任何結果:

def iftaAccountInfo = 
    CTaxAccount.findByBaseCountryAndExpireDateBetween(
     country, 
     expireDateBegin, 
     expireDateEnd, 
     [max: 1, sort: "expireDate", order: "desc"]) 

如果我使用一個dymanic僅查看國家/地區,我可以看到結果,並且在查看調試器時,日期將作爲時間戳記被投射。

設置logSql = true時SQL正確,但當GORM在查詢中設置日期時,日期正在進行。我無法弄清楚這一點。任何幫助將不勝感激。 我也嘗試用Date().ClearTime()清除時間。不用找了。

我正在運行Grails 2.3.4,OJDBC7.jar和JDK 1.7_045。 Oracle數據源設置爲ReadOnly,因爲這是我用於查找的傳統數據庫。

回答

2

你有什麼理由把日期分成幾部分?我通常使用SimpleDateFormat類並操縱從params對象接收到的字符串。

import java.text.SimpleDateFormat 

def country = params.country 
String strExpireDateBegin, strExpireDateEnd 

//This is dependent on what values you're passing to your service. 
strExpireDateBegin = (params.startDate == null)?"":params.expireDateBegin 
strExpireDateEnd = (params.endDate == null)?"":params.expireDateEnd 

//This would be how I would generate the expiration date a year from the expireBeginDate 
Date expireDateBegin = new Date() 
Date expireDateEnd = expireDateBegin + 365 


***use this for rendering the string into a Date*** 
Date expireDateBegin, expireDateEnd 
expireDateBegin = new SimpleDateFormat("yyyy-MM-dd").parse(strExpireDateBegin) 
expireDateEnd = new SimpleDateFormat("yyyy-MM-dd").parse(strExpireDateEnd) 

***results = MyCriteria.list { 
    and { 
     country = myCountry 
    } 
    between("expireDate", expireDateBegin, expireDateEnd) 
    order("expireDate", "desc") 
} 

***日期渲染

def init = { servletContext -> 
    JSON.registerObjectMarshaller(Date) { 
     return it?.format("yyyy-MMM-dd") 
    } 
} 
+0

BootStrap.groovy中的配置,我相信我嘗試這樣做。我認爲,當它將字符串解析回日期時,它的時間爲00:00:00。我會試一試雖然 –

+1

日期上的時間應該沒有關係,但是如果您需要格式化UI日期的日期。您可以使用Bootstrap.groovy中的以下內容來編輯回顯的日期。我更新了我的答案,告訴你如何修改日期。 – Wheezykw