Different and mutual conversion modes of java.sql.Date and Java.util.Date

Source: Internet
Author: User
Tags java web

A: Preface

This is my first blog in the new company, it has been one weeks, but in the past few days I really confused feeling here is not suitable for my appearance, and I came to practice, I do not want to ah, I do is Java Web, the most primitive servlet, Code and Chaos, This makes me very helpless ah, so I began to resign in Monday, then the eldest brother to talk to me, said the project is we outsource to others, we just do not have time to change it, so that at least let me feel can accept, finally I mention the next I do not want to intern, raise the wages of things also passed the , the efficiency of the company's officers is still very high AH. It's a servlet, but it's learned something else. Now we're just going to say.

II: Content

When I was doing the servlet, for example, our Vo has a birthday field, which is the date type, and when we remove the date type data from the database, Rs.getdate (1) The time we took is actually a java.sql.Date type of date, you are directly set to the Vo class is also a row, but I was to convert it to JSON data encountered a problem. Report the error as follows;

1 net.sf.json.JSONException:java.lang.reflect.InvocationTargetException2At Net.sf.json.jsonobject._frombean (jsonobject.java:987)3At Net.sf.json.JSONObject.fromObject (jsonobject.java:168)4At Net.sf.json.abstractjson._processvalue (abstractjson.java:265)5At Net.sf.json.jsonobject._processvalue (jsonobject.java:2808)6At Net.sf.json.JSONObject.processValue (jsonobject.java:2874)7At Net.sf.json.JSONObject.setInternal (jsonobject.java:2889)8At Net.sf.json.JSONObject.setValue (jsonobject.java:1577)9At Net.sf.json.jsonobject._frombean (jsonobject.java:934)TenAt Net.sf.json.JSONObject.fromObject (jsonobject.java:168) OneAt Net.sf.json.abstractjson._processvalue (abstractjson.java:265) AAt Net.sf.json.jsonarray._processvalue (jsonarray.java:2514) -At Net.sf.json.JSONArray.processValue (jsonarray.java:2539) -At Net.sf.json.JSONArray.addValue (jsonarray.java:2526) theAt Net.sf.json.jsonarray._fromcollection (jsonarray.java:1057) -At Net.sf.json.JSONArray.fromObject (jsonarray.java:123) -At Net.sf.json.JSONArray.fromObject (jsonarray.java:105) -At Com.statistics.servlet.StatisticsServlet.doGet (statisticsservlet.java:60) +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) AAt Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:208) atAt 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 Com.alibaba.druid.support.http.WebStatFilter.doFilter (webstatfilter.java:124) -At Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:241) -At Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:208) inAt Org.apache.catalina.core.StandardWrapperValve.invoke (standardwrappervalve.java:220) -At Org.apache.catalina.core.StandardContextValve.invoke (standardcontextvalve.java:122) toAt 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) theAt 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)Panax NotoginsengAt Org.apache.coyote.http11.AbstractHttp11Processor.process (abstracthttp11processor.java:1070) -At Org.apache.coyote.abstractprotocol$abstractconnectionhandler.process (abstractprotocol.java:611) theAt Org.apache.tomcat.util.net.jioendpoint$socketprocessor.run (jioendpoint.java:314) + At Java.util.concurrent.ThreadPoolExecutor.runWorker (Unknown Source) A At Java.util.concurrent.threadpoolexecutor$worker.run (Unknown Source) theAt Org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run (taskthread.java:61) + At Java.lang.Thread.run (Unknown Source) - caused By:java.lang.reflect.InvocationTargetException $ At sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) $ At Sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source) - At Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source) - At Java.lang.reflect.Method.invoke (Unknown Source) theAt Org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod (propertyutilsbean.java:1773) -At Org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty (propertyutilsbean.java:1132)WuyiAt Org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty (propertyutilsbean.java:686) theAt Org.apache.commons.beanutils.PropertyUtilsBean.getProperty (propertyutilsbean.java:715) -At Org.apache.commons.beanutils.PropertyUtils.getProperty (propertyutils.java:290) WuAt Net.sf.json.jsonobject._frombean (jsonobject.java:918) -... 41 More About caused By:java.lang.IllegalArgumentException $ At java.sql.Date.getHours (Unknown Source) -... Wuyi more
View Code

I was debugging the breakpoint only to know when the conversion was reported errors, and finally found that the reason for this date. I'll write something about the JSON transformation in the next article http://www.cnblogs.com/wuhao1991/p/4172385.html. You have now found four ways to convert JSON or jar.

Let's talk about the date conversion first.

Just read the next JDK document, only to know that the original java.sql.Date is inherited java.util.Date

Can look at some of its methods. Now let's look at the code

1  PackageOrg.wh.DateDemo;2 3 ImportJava.sql.Timestamp;4 ImportJava.text.SimpleDateFormat;5 6  Public classSqldatedemo {7 8      Public Static voidMain (string[] args) {9Java.util.Date utildate=Newjava.util.Date ();TenSystem.out.println ("Util Date:" +utildate); One          AJava.sql.Date sqldate=Newjava.sql.Date (Utildate.gettime ()); -System.out.println ("Date for sql:" +sqldate); -          theJava.util.Date stou=Newjava.util.Date (Sqldate.gettime ()); -System.out.println ("Sqldate converted to Utildate" +sTOu); -          -          +Timestamp ts=NewTimestamp (Utildate.gettime ()); -System.out.println ("Timestamp:" +ts); +          AJava.util.Date ttou=Newjava.util.Date (Ts.gettime ()); atSystem.out.println ("Ttou:" +Ttou); -          -Java.sql.Date sqldate1=java.sql.date.valueof ("2014-12-10")); - System.out.println (sqlDate1); -          -SimpleDateFormat f=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); inSimpleDateFormat f1=NewSimpleDateFormat ("yyyy"); -SYSTEM.OUT.PRINTLN ("Data after format:" +F.format (Ttou)); toSYSTEM.OUT.PRINTLN ("Data after format:" +F1.format (Ttou)); +          -     } the  *}
View Code

The results of the operation are as follows:

Util Date:thu Dec 14:58:53 CSTfor SQL Date:2014-12-1814:58:53 CSTTimestamp: 2014-12-18 14:58:53.26514:58:53 CST 20142014-12-10Format data:data after 2014-12-18 02:58:53 format :2014

In fact, I've been struggling with the insertion of date dates, if the field in the database is of type date, then the value inserted into the database after inserting today's date is "2014-12-18", and the Java code does not have to be converted to the "YYYY-MM-DD" type. Date type.

If you can use the JSTL label when the interface is displayed

<fmt:formatdate value= "${list.birthday}" pattern= "Yyyy-mm-dd"/>

Three: summary

Today I have to hand over the name of graduation design, I am very tangled ah, but I suddenly came to the inspiration, I want to do a charging pile management system, because recently is the company in the charge pile control, and I saw the "luxury car" Tesla in China's deployment, the big cities are equipped with a charging pile device. There are more than 40 in Asia, there are more than 30 charging piles in China, so let's do the statistics. The second title is to do a car management system. There is really nothing to do.

Java.sql.Date and java.util.Date different and mutual conversion modes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.