Java 今天就开始';时间戳中的日期
我在postgreSQL中有一个函数,根据它的参数返回0或1,我有一个方法可以访问这个函数,但是当我运行时,我得到了错误,我在设置时间戳中的当前日期时遇到了问题。我试着用simpleDataFormat和其他很多东西来添加模式,但我做不到。提前谢谢Java 今天就开始';时间戳中的日期,java,postgresql,Java,Postgresql,我在postgreSQL中有一个函数,根据它的参数返回0或1,我有一个方法可以访问这个函数,但是当我运行时,我得到了错误,我在设置时间戳中的当前日期时遇到了问题。我试着用simpleDataFormat和其他很多东西来添加模式,但我做不到。提前谢谢 ERROR: function inserir_posicao(numeric, bigint, double precision, double precision, numeric) does not exist Dica: No func
ERROR: function inserir_posicao(numeric, bigint, double precision, double precision, numeric) does not exist
Dica: No function matches the given name and argument types. You might need to add explicit type casts.
函数(以db为单位):
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, timestamp without time zone, double precision, double precision, bigint)
OWNER TO postgres;
方法:
public int inserirPosicao(BigInteger tagId, BigInteger gadoId, double lat, double lon) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Query qry = manager.createNativeQuery("select inserir_posicao(:tag,:data,:lat,:lng,:gado)");
qry.setParameter("tag", tagId);
qry.setParameter("data", timestamp.getTime());
qry.setParameter("lat", lat);
qry.setParameter("lng", lon);
qry.setParameter("gado", gadoId);
return (int) qry.getSingleResult();
}
您的问题在这一行:
qry.setParameter("data", timestamp.getTime());
该方法返回自1970年1月1日以来的日期/时间(以毫秒为单位),这是一个大整数。但是您的函数需要一个时间戳值,所以您得到了您所说的这个错误
解决方案:
您必须将“日期/时间”值作为格式化字符串传递,如“yyyy-mm-dd-hh24:mi:ss”,然后将参数设置为字符串
。。。或者,如果您仍希望以毫秒为单位传递日期,则必须通过更改以下行将此bigint数字“转换”为日期/时间:
Query qry = manager.createNativeQuery("select inserir_posicao(:tag,:data,:lat,:lng,:gado)");
到
我们必须将:data
除以1000
的原因是to_timestamp
期望自1970年1月1日起的秒数,而您正在传递毫秒数
_timestamp函数的单个参数也可用;它接受
双精度参数,并从Unix历元(秒)转换
从1970-01-01 00:00:00+00)到带有时区的时间戳。(整数
Unix时代隐式转换为双精度。)
您的问题可能重复出现在这一行:qry.setParameter(“data”,timestamp.getTime());请检查我的答案。谢谢你的帮助!我明白了
Query qry = manager.createNativeQuery("select inserir_posicao(:tag, to_timestamp(:data / 1000) ,:lat,:lng,:gado)");