Java.sql.Timestamp in database converted to date

Source: Internet
Author: User

The time type in the query database is Java.sql.Timestamp

Save in JSON requires formatting

Custom Tool Class Datejsonvalueprocessor

1  PackageCom.rom.util;2  3 ImportJava.text.SimpleDateFormat;4  5 Importjava.util.Date;6 ImportJava.util.Locale;7  8 ImportNet.sf.json.JsonConfig;9 ImportNet.sf.json.processors.JsonValueProcessor;Ten   One  Public classDatejsonvalueprocessorImplementsJsonvalueprocessor { A   -     PrivateString Datepattern = "Yyyy-mm-dd"; -   the      Publicdatejsonvalueprocessor () { -         Super(); -     } -   +      Publicdatejsonvalueprocessor (String format) { -         Super(); +          This. Datepattern =format; A     } at   -      PublicObject Processarrayvalue (object value, Jsonconfig jsonconfig) { -         returnprocess (value); -     } -   -      PublicObject Processobjectvalue (String key, object value, in jsonconfig Jsonconfig) { -         returnprocess (value); to     } +   -     PrivateObject Process (object value) { the         Try { *             if(ValueinstanceofDate) {    $SimpleDateFormat SDF =NewSimpleDateFormat (Datepattern, locale.uk);Panax Notoginseng                 returnSdf.format ((Date) value); -             } the             return""; +}Catch(Exception e) { A             return""; the         } +   -     } $   $      PublicString Getdatepattern () { -         returnDatepattern; -     } the   -      Public voidSetdatepattern (String pdatepattern) {WuyiDatepattern =Pdatepattern; the     } -   Wu}

The instanceof operator in Java is used to indicate at run time whether an object is an instance of a particular class. Instanceof Indicates whether this object is an instance of this particular class or its subclasses by returning a Boolean value.
usage :
Result = Object Instanceof class
Parameters :
Result: Boolean type.
Object: Required option. An arbitrary object expression.
Class: Required option. Any of the defined object classes.
Description :
If object is an instance of class, the instanceof operator returns TRUE. Returns False if object is not an instance of the specified class, or if object is null.

The colleague who saved the JSON data calls Jsonconfig

1 New jsonconfig (); 2 jsonconfig.registerjsonvalueprocessor (Timestamp.  Classnew datejsonvalueprocessor ("Yyyy-mm-dd")); 3 Jsonarray jo = Jsonarray.fromobject (list,jsonconfig);

The program automatically converts the format to java.util.Date form

such as YYYY-MM-DD or YYYY-MM-DD HH:mm:ss

Java.sql.Timestamp in database converted to date

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.