postgresql在Java中获取不带时区的时间
我在postgreSQL中有一个函数,它有一个没有时区列的时间。然后在我的方法中,我在设置参数时出错。它说这种类型是未知的。如何使用当前系统时间设置此参数?该方法的目的是验证数据库中是否存在寄存器
功能:
CREATE OR REPLACE FUNCTION public.inserir_posicao(
_tag bigint,
_data_hora timestamp without time zone,
_lat double precision,
_long double precision,
_gado_id bigint)
RETURNS integer AS
$BODY$declare
tagPesq BigInt;
begin
select tag into tagPesq from coordenadas where tag = $1;
if tagPesq is not null and tagPesq > 0 then
update coordenadas set pos_data = $2,
pos_latitude = $3,
pos_longitude = $4,
gado_id = $5
where tag_id = $1;
else
insert into coordenadas(pos_data,pos_latitude,pos_longitude,
tag_id, gado_id) values ($2,$3,$4,$1,$5);
end if;
return 1;
EXCEPTION WHEN RAISE_EXCEPTION THEN
BEGIN
return 0;
END;
end;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION public.inserir_posicao(bigint, time with time zone, double precision, double precision, bigint)
OWNER TO postgres;
方法:
public int insertPosicao(BigInteger tagId, BigInteger gadoId, double lat, double lon) {
Query qry = manager.createNativeQuery("select inserir_posicao(:tag,:data,:lat,:lng,:gado)");
qry.setParameter("tag", tagId);
qry.setParameter("data", ??? ); //this parameter.
qry.setParameter("lat", lat);
qry.setParameter("lng", lon);
qry.setParameter("gado", gadoId);
return (int) qry.getSingleResult();
}
# 1 楼答案
仅在Java 8中,您可以使用以下命令来获取UTC的本地时间偏移,并且不带区域:
如果您有自己的日期,可以使用相同的代码,只需将
Instant.now()
替换为Instant.parse(date)
,尽管它必须正确格式化# 2 楼答案
嗯,你可以用
您必须指定您的模式
在我的代码中,我放置了返回当前时间的
GregorianCalendar.getInstance
。您也可以通过一个sysdate
on查询来实现这一点。 关于你的功能,关于pos_data = $2
您必须设置pos_data=to_date($2,**pattern**)
,因为现在,您传递一个代表日期的字符串,并通过postgres函数将其转换为日期另见:SimpleDateFormat和to_date PostgreSQL