No delimiter date string processing in Java "JSON"

Source: Internet
Author: User

The date type data processing in Json, the date of the server transmission is no delimiter, the general format is two, [20151212121212] namely YYYYMMDDHHMMSS and [121212]hhmmss

ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.Locale; Public classTest { Public Static voidMain (string[] args) {String strdatetime= "20150206201756"; String Strtime= "201756"; System.out.println (Convjsondatetostring (Strdatetime,"Yyyy-mm-dd Hh:mm:ss")); System.out.println (Convjsondatetostring (Strtime,"Hhmmss", "Hh:mm:ss")); }     Public Staticstring convjsondatetostring (String jsondate,string stringtemplate) {string resultstring=NULL; Try{Calendar Cldcalendar=convtocalender (Jsondate, "YYYYMMDDHHMMSS"); Resultstring=convtostring (Cldcalendar, stringtemplate); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnresultstring; }     Public Staticstring convjsondatetostring (String jsondate,string jsontemplate,string stringtemplate) {string ResultS Tring=NULL; Try{Calendar Cldcalendar=Convtocalender (Jsondate, jsontemplate); Resultstring=convtostring (Cldcalendar, stringtemplate); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnresultstring; }    //turns date string in specified format to date type     Public StaticCalendar convtocalender (String str,string template) {SimpleDateFormat SDF;        Date date; Calendar Cltresult=calendar.getinstance (); SDF=NewSimpleDateFormat (template, Locale.getdefault ()); Try{Date=sdf.parse (str);        Cltresult.settime (date); } Catch(Exception ex) {//TODO auto-generated Catch blockEx.printstacktrace (); }        returnCltresult; }    //turn the date to the specified format string     Public Staticstring convtostring (Calendar cld,string template) {string resultstring=NULL; Try{Date Date=Cld.gettime (); SimpleDateFormat SDF=NewSimpleDateFormat (Template,locale.getdefault ()); Resultstring=Sdf.format (date); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnresultstring; }}

No delimiter date string processing in Java "JSON"

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.