【问题标题】:Problem with String to Date Conversion字符串到日期转换的问题
【发布时间】: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


【解决方案1】:

日期对象本身没有格式化,它只返回日期和时间值。您可以通过仅支持格式的类以字符串格式(如 SimpleDateFormat、DateFormat 等)获得格式化。

【讨论】:

    【解决方案2】:

    为什么要使用 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

    【讨论】:

    • groovy.lang.MissingMethodException:没有方法签名:com.springpeople.steer.trips.TripController.after() 适用于参数类型:(java.lang.String, java.sql.Date ) 值:[contract_begins, 2011-08-12]
    • 我修复了代码 - 没有尝试过,但它可以 :-)
    • [2011-08-12 18:22:58:203] 错误 groovy.grails.web.errors.GrailsExceptionResolver ### 无法解析属性:client_id of: com.springpeople.steer.rules .Contract org.hibernate.QueryException:无法解析属性:client_id of:com.springpeople.steer.rules.Contract
    • 客户端定义在规则而不是合同中,合同扩展规则
    • 好吧,如果 Contract 扩展了 Rule,那么它应该可以在 Contract 中访问,因此它应该是可查询的。我没有说我的代码已经过测试,但它是一个例子。请阅读文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 2017-03-15
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多