java.lang.NumberFormatException:对于输入字符串:

时间:2013-09-01 05:29:12

标签: java hibernate postgresql types number-formatting

我正在尝试将String时间戳转换为Integer,我将postgres用作DB。

@Column(name = "connecte_timestamp")
private Integer timestamp;

SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
Date aujourdhui = new Date();
this.timestamp = Integer.parseInt(formater.format(aujourdhui)
                .replace("-", "").replace(" ", "").replace(":", ""));

时间戳在数据库中有bigint作为type。 当我运行我的应用程序时,我得到以下堆栈跟踪:

  

java.lang.NumberFormatException:对于输入字符串:“01092013062024”     at java.lang.NumberFormatException.forInputString(Unknown Source)at   java.lang.Integer.parseInt(未知来源)at   java.lang.Integer.parseInt(未知来源)at   com.forum.beans.Connecte。(Connecte.java:26)at   com.forum.servlets.ListageForums.doGet(ListageForums.java:32)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:621)at at   javax.servlet.http.HttpServlet.service(HttpServlet.java:728)at at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)     在   org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:589)     在   org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在java.lang.Thread.run(未知来源)

请帮忙吗?

1 个答案:

答案 0 :(得分:5)

1092013062024太大而无法int使用long。我也会做一点点不同

private Long timestamp;

SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddhhmmss");
timestamp = Long.parseLong(formater.format(new Date));

我认为yyyyMMddhhmmss格式更符合逻辑,它使比较和排序变得简单