如何使用jpa2条件查询或jpql获取postgresql日期字段的月份和年份?
有人这样做过吗?
答案 0 :(得分:2)
使用此:FUNC('MyFunc', a, b, c)
示例:SELECT FUNC('to_char', current_date, 'month')
答案 1 :(得分:1)
一些例子,如何从PostgreSQL中的日期或时间戳中提取月份:
select to_char(now(), 'month');
select to_char(now(), 'MM');
select extract('month' from now());
select date_part('month', now());
答案 2 :(得分:0)
使用JPA条件API(仅使用Hibernate测试):
// Create expressions that extract date parts:
Expression<Integer> year = cb.function("year", Integer.class, date);
Expression<Integer> month = cb.function("month", Integer.class, date);
Expression<Integer> day = cb.function("day", Integer.class, ts);
// Create expressions that extract time parts:
Expression<Integer> hour = cb.function("hour", Integer.class, time);
Expression<Integer> minute = cb.function("minute", Integer.class, time);
Expression<Integer> second = cb.function("second", Integer.class, ts);
来源:http://www.objectdb.com/java/jpa/query/jpql/date#Date_and_Time_in_Criteria_Queries_
使用jpql(仅使用Hibernate测试):
YEAR(date)
MONTH(date)
DAY(date)
HOUR(date)
MINUTE(date)
SECOND(date)