本文介绍了在Java中获得时间没有时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在postgreSQL中有一个没有时区列的功能。然后在我的方法中,设置参数时会出错。它说类型是未知的。如何使用当前系统时间设置此参数?该方法的目的是验证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)返回整数AS $ BODY $ declare tagPesq BigInt; begin 从标签= $ 1的coordenadas中选择标签到tagPesq; 如果tagPesq不为null并且tagPesq> 0然后更新coordenadas set pos_data = $ 2, pos_latitude = $ 3, pos_longitude = $ 4, gado_id = $ 5 其中tag_id = $ 1; else 插入coordenadas(pos_data,pos_latitude,pos_longitude, tag_id,gado_id)值($ 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,时区与时区,双精度,双精度,bigint) OWNER TO postgres; 方法: code> public int insertPosicao(BigInteger tagId,BigInteger gadoId,double lat,double lon){ 查询qry = manager.createNativeQuery(select insertedir_posicao(:tag,:data,:lat, :lng,:gado)); qry.setParameter(tag,tagId); qry.setParameter(data,???); //这个参数。 qry.setParameter(lat,lat); qry.setParameter(lng,lon); qry.setParameter(gado,gadoId); return(int)qry.getSingleResult(); } 解决方案嗯,你可以使用像 SimpleDateFormat sdf = new SimpleDateFormat(** pattern **); String sData = sdf.format(GregorianCalendar.getInstance()); qry.setParameter(date,sData); 您必须指定模式 在我的代码中,我把 GregorianCalendar.getInstance 返回当前时间。您也可以使用 sysdate 来查询。 在你的函数上,在 pos_data = $ 2 你必须设置 pos_data = to_date($ 2,** pattern **)因为现在,你传递一个String代表日期,并通过postgres函数转换为日期 另请参见: SimpleDateFormat 和 to_date PostgreSQL I have a function in postgreSQL that has a time without time zone column. Then in my method, I get an error when setting the parameter. It says the type is unknown. How can I set this parameter with the current system time? The purpose of the method is to verify if the register exists or not in DB.Function: 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;Method: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(); } 解决方案 Umh you could use something likeSimpleDateFormat sdf = new SimpleDateFormat(**pattern**);String sData = sdf.format(GregorianCalendar.getInstance());qry.setParameter("date",sData);where you have to specify you patternIn my code I put GregorianCalendar.getInstance that return current time. You can also achieve this with just a sysdate on query.On your function, onpos_data = $2 you have to set pos_data=to_date($2,**pattern**) because now, you pass a String stands for date and via postgres function you transfor it into dateSee also: SimpleDateFormat and to_date PostgreSQL 这篇关于在Java中获得时间没有时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-20 23:18