[英]Change date format criteria query in hibernate?
I am displaying my test name and date by using below query. 我通过使用以下查询显示我的考试名称和日期。 Now my date format is 2014-02-27 17:26:49.0
but i need to display in 2014-02-27
format. 现在我的日期格式为2014-02-27 17:26:49.0
但我需要以2014-02-27
格式显示。 How can i modify my query for this? 我该如何修改我的查询?
Session ses = sessionFactory.getCurentSession();
Criteria c = ses.createCriteria(user.class);
c.add(Restrictions.eq("id", uid));
c.add(Restrictions.eq("name", uname));
ProjectionList pl = Projections.projectionList();
pl.add(Projections.property("testid"));
pl.add(Projections.property("date"));
c.setProjection(Projections.distinct(pl));
List<String> list = c.list();
The code above is incorrect. 上面的代码不正确。 It selects two fields, and you assume that it returns a List<String>
. 它选择两个字段,并假定它返回List<String>
。 That is not possible. 这是不可能的。 Every element of the list will be an Object array containing 2 elements: the testid, and the date. 列表中的每个元素都是一个包含2个元素的Object数组:testid和date。 The date element will be of the same type as the date field in your entity (which, I hope, is java.util.Date
). date元素将与您实体中的date字段具有相同的类型(我希望是java.util.Date
)。
So, to display the result of the query using a specific format, you would use something like the following: 因此,要使用特定格式显示查询结果,您将使用类似以下的内容:
DateFormat df = new SimpleDateFormat("MMMM dd, yyyy");
List<Object[]> rows = c/list();
for (Object[] row : rows) {
Date date = (Date) row[1];
System.out.println(row[0] + " - " + df.format(date);
}
By using "sqlGroupProjection" I have changed my date format to **2014-02-27**
projectionList.add(Projections.sqlGroupProjection("date(date) as createdDate", "createdDate", new String[] { "createdDate" }, new Type[] { StandardBasicTypes.DATE }));
Try this. 尝试这个。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
Date inputDate = new Date();
System.out.println(simpleDateFormat.format(inputDate));
}
output : 输出:
27/02/2014 07:25
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("MMMM dd,yyyy");
Date date = format1.parse("2014-02-27 17:26:49.00");
System.out.println(format2.format(date));
INPUT : 2014-02-27 17:26:49.00
OUTPUT : February 27,2014
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.