[英]passing date object to hibernate query
Is there anyway to specify the Date format in Java? 反正有没有用Java指定日期格式? I'm not asking about string formatting. 我不是在问字符串格式。 When I inspect a date value (in debug mode) in my REST services, it looks like follows: 当我在REST服务中检查日期值(在调试模式下)时,看起来如下:
Mon Sep 01 00:00:00 IST 2014
When I pass this date to a hibernate query, it doesn't return me any results, event though there's valid results that satisfies the criteria. 当我将此日期传递给休眠查询时,即使有满足标准的有效结果,它也不会返回任何结果。 Following is my hibernate query: 以下是我的休眠查询:
select this_.ID as ID1_59_0_, this_.SEVERITY as SEVERITY5_59_0_, this_.EVENT as EVENT2_59_0_, this_.PROCESS_NAME as PROCESS3_59_0_, this_.HOST_NAME as HOST6_59_0_, this_.TIME as TIME4_59_0_
from SYSTEM_LOG this_
where this_.TIME>? and this_.TIME<?
order by this_.TIME asc
I run this query in mySQL and pass pass the date as follows, it doesn't return any data, which I believe is what's happening in the application: 我在mySQL中运行此查询并传递日期,如下所示,它不返回任何数据,我相信这是应用程序中正在发生的事情:
select this_.ID as ID1_59_0_, this_.SEVERITY as SEVERITY5_59_0_, this_.EVENT as EVENT2_59_0_, this_.PROCESS_NAME as PROCESS3_59_0_, this_.HOST_NAME as HOST6_59_0_, this_.TIME as TIME4_59_0_
from SYSTEM_LOG this_
where this_.TIME> 'Mon Sep 01 00:00:00 IST 2014' and this_.TIME<'Mon Sep 11 00:00:00 IST 2014'
order by this_.TIME asc;
But if I change the date as follows, the query returns data: 但是,如果我按如下方式更改日期,查询将返回数据:
select this_.ID as ID1_59_0_, this_.SEVERITY as SEVERITY5_59_0_, this_.EVENT as EVENT2_59_0_, this_.PROCESS_NAME as PROCESS3_59_0_, this_.HOST_NAME as HOST6_59_0_, this_.TIME as TIME4_59_0_
from SYSTEM_LOG this_
where this_.TIME> '2014/9/1' and this_.TIME<'2014/9/11'
order by this_.TIME asc;
How do I use the correct date format? 如何使用正确的日期格式? I tried doing the following, but still the date is shown in same format: 我尝试执行以下操作,但日期仍以相同格式显示:
DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
Date sDate = formatter.parse(startDate);
Date eDate = formatter.parse(endDate);
Update: FOllowing is the hibernate method I use to retrieve the data: 更新:跟随是我用来检索数据的休眠方法:
public PagedDataResult getSystemLogsByDate(Date startDate, Date endDate, String sortColumn, int searchBySeverity,
String searchByProcessName, String searchByHostName, String searchByDescription,
int page, int pageSize) {
String sortField = "time";
List<Criterion> criteria = new ArrayList<Criterion>();
criteria.add(Restrictions.gt(sortField, startDate));
criteria.add(Restrictions.lt(sortField, endDate));
if(searchBySeverity >= 0)
criteria.add(Restrictions.eq("alarmSeverities.id", searchBySeverity));
if(!searchByProcessName.isEmpty())
criteria.add(Restrictions.like("processName", searchByProcessName));
if(!searchByHostName.isEmpty())
criteria.add(Restrictions.like("servers.hostName", searchByHostName));
if(!searchByDescription.isEmpty())
criteria.add(Restrictions.like("event", searchByDescription));
return (PagedDataResult)getAllBySort(sortColumn, page, pageSize, criteria);
}
what i understand is you have a date as String as you have speciified and you want to use it to a select query then you need to change it to sql format date in the following way 我的理解是,您已将日期指定为String,并且想要将其用于选择查询,然后需要按以下方式将其更改为sql格式的日期
First transform from StringDate to java.util.Date 首先从StringDate转换为java.util.Date
public static java.util.Date transformFromStringToDate(String dateInString){
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
java.util.Date utilDate = null;
try {
if(dateInString != null && !(dateInString.equals(""))) {
utilDate = dateFormat.parse(dateInString);
}
} catch (ParseException e) {
e.printStackTrace();
}
return utilDate;
}
Then convert the java.util.Date to java.sql.Date 然后将java.util.Date转换为java.sql.Date
public static java.sql.Date convertUtilDateToSqlDate(java.util.Date date){
if(date != null && !(date.equals(""))) {
Date sqlDate = new Date(date.getTime());
return sqlDate;
}
return null;
}
Finally use the java.sql.Date in you query for the PreparedStatement 最后,在查询中的java.sql.Date中使用PreparedStatement
If you directly have java.util.Date then skip the first step 如果您直接拥有java.util.Date,则跳过第一步
Edit 编辑
if the column is a timestamp
you need to convert the java.util.Date
to java.sql.Timestamp
like this 如果列是timestamp
,则需要像这样将java.util.Date
转换为java.sql.Timestamp
public java.sql.Timestamp convertUtilDateToSqlTimestamp(java.util.Date date){
if(date != null && !(date.equals(""))) {
Timestamp sqlTimestamp = new Timestamp(date.getTime());
return sqlTimestamp ;
}
return null;
}
public PagedDataResult getSystemLogsByDate(Date startDate, Date endDate,..) {
java.sql.Timestamp startTimestamp = new YourClassName()
.convertUtilDateToSqlTimestamp(startDate);
java.sql.Timestamp endTimestamp = new YourClassName()
.convertUtilDateToSqlTimestamp(endDate);
...
criteria.add(Restrictions.gt(sortField, startTimestamp));
criteria.add(Restrictions.lt(sortField, endTimestamp));
...
}
Have this method convert the Date to sql timestamp and use the convert timestamp for querying 让此方法将Date转换为sql时间戳,并使用convert时间戳进行查询
Here is some code that I will retrieve using TimeStamp 这是一些我将使用TimeStamp检索的代码
java.util.Date date = getItSomehow();
Timestamp timestamp = new Timestamp(date.getTime());
preparedStatement = connection.prepareStatement("SELECT * FROM tbl WHERE ts > ?");
preparedStatement.setTimestamp(1, timestamp);
the method getItSomehow
will convert your String to a Date using SimpleDateFormat. 方法getItSomehow
将使用SimpleDateFormat将您的String转换为Date。
based upon the String that you are getting from your REST system, the mask would be 根据您从REST系统获取的字符串,掩码将为
EEE MMM HH:mm:ss yyyy Z -> Mon Sep 01 00:00:00 IST 2014
based upon your updated code, try 根据您更新的代码,尝试
criteria.add(Restrictions.gt(sortField, new Timestamp(startDate.getTime ())));
criteria.add(Restrictions.lt(sortField, new Timestamp(endDate.getTime ())));
That's maybe because you're using java.util.Date
instead of java.sql.Date
. 那可能是因为您使用的是java.util.Date
而不是java.sql.Date
。 Try changing your import to java.sql.Date
and see if it works out. 尝试将导入更改为java.sql.Date
,看看是否可以解决。
By default mySql uses the Swedish locale and all dates must be submitted as '2014-08-26'. 默认情况下,mySql使用瑞典语区域设置,所有日期都必须提交为“ 2014-08-26”。 Change your date formatter to SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); 将日期格式更改为SimpleDateFormat(“ yyyy-MM-dd”,Locale.ENGLISH);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.