无论如何在Java中指定日期格式?我不是在询问字符串格式。当我在我的REST服务中检查日期值(在调试模式下)时,它看起来如下:
Mon Sep 01 00:00:00 IST 2014
当我将此日期传递给hibernate查询时,它不会返回任何结果,尽管有符合条件的有效结果。以下是我的hibernate查询:
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
我在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;
但如果我按如下方式更改日期,则查询将返回数据:
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;
如何使用正确的日期格式?我尝试了以下操作,但日期仍然以相同的格式显示:
DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
Date sDate = formatter.parse(startDate);
Date eDate = formatter.parse(endDate);
更新: FOllowing是我用来检索数据的hibernate方法:
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);
}
答案 0 :(得分:0)
我所理解的是你有一个日期为String,因为你已经指定了,并且你想将它用于一个选择查询,那么你需要以下面的方式将它改为sql格式日期
首先从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;
}
然后将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;
}
最后在查询PreparedStatement
时使用java.sql.Date如果直接使用java.util.Date,则跳过第一步
修改
如果列是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));
...
}
让此方法将Date转换为sql时间戳并使用转换时间戳进行查询
答案 1 :(得分:0)
以下是我将使用TimeStamp
检索的一些代码java.util.Date date = getItSomehow();
Timestamp timestamp = new Timestamp(date.getTime());
preparedStatement = connection.prepareStatement("SELECT * FROM tbl WHERE ts > ?");
preparedStatement.setTimestamp(1, timestamp);
方法getItSomehow
将使用SimpleDateFormat将您的String转换为日期。
基于您从REST系统获取的String,掩码将是
EEE MMM HH:mm:ss yyyy Z -> Mon Sep 01 00:00:00 IST 2014
根据您更新的代码,尝试
criteria.add(Restrictions.gt(sortField, new Timestamp(startDate.getTime ())));
criteria.add(Restrictions.lt(sortField, new Timestamp(endDate.getTime ())));
答案 2 :(得分:-1)
这可能是因为您使用的是java.util.Date
而不是java.sql.Date
。尝试将导入更改为java.sql.Date
并查看其是否有效。
答案 3 :(得分:-1)
默认情况下,mySql使用瑞典语语言环境,所有日期必须以“2014-08-26”提交。将日期格式化程序更改为SimpleDateFormat(“yyyy-MM-dd”,Locale.ENGLISH);