java.sql.Date to java.util.Date

  发这篇博文的题目可能无法直接表示内容,但是确实是java.sql.Date和java.util.Date.

  今天在使用‘net.sf.json.JSONObject‘封装json数据的时候,碰到很奇怪的问题,报出的异常,让我开始觉得,不知道从哪个异常开始找解决方案,报错如下:

一月 30, 2015 11:14:49 上午 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [spt.servlet.JsonServlet] in context with path [/ServerAQI] threw exception
net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
	at net.sf.json.JSONObject._fromBean(JSONObject.java:953)
	at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
	at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
	at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)
	at net.sf.json.JSONObject.setValue(JSONObject.java:1507)
	at net.sf.json.JSONObject._fromBean(JSONObject.java:940)
	at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
	at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
	at net.sf.json.JSONObject.processValue(JSONObject.java:2833)
	at net.sf.json.JSONObject.element(JSONObject.java:1871)
	at net.sf.json.JSONObject.element(JSONObject.java:1849)
	at net.sf.json.JSONObject.put(JSONObject.java:2466)
	at spt.service.JsonService.getJsonStr(JsonService.java:12)
	at spt.servlet.JsonServlet.doPost(JsonServlet.java:34)
	at spt.servlet.JsonServlet.doGet(JsonServlet.java:53)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at spt.servlet.filter.EncodingFilter.doFilter(EncodingFilter.java:24)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)
	at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1132)
	at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
	at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
	at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)
	at net.sf.json.JSONObject._fromBean(JSONObject.java:928)
	... 40 more
Caused by: java.lang.IllegalArgumentException
	at java.sql.Date.getHours(Date.java:182)
	... 50 more

  通常,我们有两种思路找解决方案:从我们自定义类中开始找错.

  这种思路是我在网上看教程视频,一个讲师说的,蛮实用的.但是一开始的时候,我并没有这样做(其实我应该这样做).我先是查 ‘java.lang.IllegalArgumentException java.sql.Date.getHours),发现有个哥们写的一大篇中文,但觉得他说的都不怎么对.

  又一次觉得 stackoverflow上的哥们真的很厉害.

  我们知道,java.sql.Date是java.util.Date的子类,所以我们无需显示将java.sql.Date转换为java.util.Date.

  在jdbc中,使用PreparedStatement往数据库中存储date的时候,我们需要将java.util.Date转换为java.sql.Date.

  java.sql.Date在数据库中存储的是不带time部分的,所以将java.sql.Date转换为java.util.Date的时候,应该使用 ResultSet..getTimestamp("currentDate"), 而不能让其隐式转换(ref:Converting java.sql.Date to java.util.Date)

当我使用ResultSet..getTimestamp("currentDate")将java.sql.Date转换为java.util.Date的时候,就能将json数据正常封装了.

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。