我使用以下查询显示我的测试名称和日期。现在我的日期格式是2014-02-27 17:26:49.0
,但我需要以2014-02-27
格式显示。我该如何修改我的查询?
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();
答案 0 :(得分:0)
上面的代码不正确。它选择两个字段,并假设它返回List<String>
。这是不可能的。列表的每个元素都是一个包含2个元素的Object数组:testid和日期。日期元素的类型与实体中的日期字段相同(我希望是java.util.Date
)。
因此,要使用特定格式显示查询结果,您可以使用以下内容:
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);
}
答案 1 :(得分:0)
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 }));
答案 2 :(得分:-1)
试试这个。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
Date inputDate = new Date();
System.out.println(simpleDateFormat.format(inputDate));
}
输出:
27/02/2014 07:25
答案 3 :(得分:-1)
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