有沒有辦法在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);
更新: 下面是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);
}
如何在數據庫中定義的日期?你在java代碼中使用了PreparedStatement嗎?如果是這樣設置參數使用java.sql.Date – 2014-09-01 04:31:00
日期是一個時間戳 – devC 2014-09-01 06:27:50
乾杯的信息CHAT = 2013,請參閱我的回答 – 2014-09-01 06:42:44