The topic of this blog post may not be directly expressed, but it is java.sql.Date and java.util.Date.
Today when using the ' Net.sf.json.JSONObject ' package JSON data, encountered a very strange problem, reported the exception, let me start to feel, do not know from which exception began to find a solution, the error is as follows:
January 30, 2015 11:14:49 am org.apache.catalina.core.StandardWrapperValve invokeSEVERE:Servlet.service () for Servlet [ Spt.servlet.JsonServlet] In the context with path [/serveraqi] threw exceptionnet.sf.json.JSONException: Java.lang.reflect.InvocationTargetExceptionat 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 : At Java.lang.Thread.run (thread.java:722) caused By:java.lang.reflect.InvocationTargetExceptionat 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) ... morecaused By:java.lang.IllegalArgumentExceptionat java.sql.Date.getHours (date.java:182) ... More
In general, we have two ways to find a solution: Starting with our custom class.
This idea is that I watch the tutorial video online, a lecturer said, pretty practical. But at first I didn't do it (I should). I'm looking for ' java.lang.IllegalArgumentException. java.sql.Date.getHours), found a friend wrote a large piece of Chinese, but think he said is not how right.
Once again think StackOverflow on the elder brother really is very formidable.
We know that Java.sql.Date is a subclass of Java.util.Date, so we don't need to show java.sql.Date to Java.util.Date.
In JDBC, when using PreparedStatement to store Date in a database, we need to convert java.util.Date to java.sql.Date.
Java.sql.Date is stored in the database without the time part, so when converting java.sql.Date to java.util.Date, you should use ResultSet. Gettimestamp ("currentdate") and cannot be implicitly converted (ref:converting java.sql.Date to Java.util.Date)
When I use ResultSet. When Gettimestamp ("currentdate") converts java.sql.Date to Java.util.Date, the JSON data can be properly encapsulated.
Java.sql.Date to Java.util.Date