Do the tools that are commonly used in Java EE projects!

Source: Internet
Author: User
Tags dateformat

One: Convert time Date type to string type

  1. 1 /** 2 * Convert date type to String type3      * @paramDate4      * @return 5      */  6      Public StaticString datetostring (date date) {7DateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 8         returnFormat.format (date); 9}

Two: Convert string type to date type

1     /** 2 3 * Convert String type to date type4      * @paramDate5      * @return 6      */  7      Public StaticDate stringtodate (String date) {8DateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 9Date d =NULL; Ten         Try {   OneD =format.parse (date);  A}Catch(java.text.ParseException e) { - E.printstacktrace ();  -         }   the         returnD;  -}

Three: Simultaneous judgment of "'" and null two cases

1 /** 2 * Determine if the variable is empty3      * @params4      * @return 5      */  6      Public Static BooleanIsEmpty (String s) {7         if(NULL= = S | | ". Equals (s) | |" ". Equals (S.trim ()) | | "NULL". Equalsignorecase (s)) {  8             return true; 9}Else{  Ten             return false;  One         }   A}

Four: Remove the space before and after the string

1     /** 2 * Remove spaces before and after a string3      * @params4      * @return 5      */  6      Public Staticstring Trim (string s) {7         if(NULL==s) {  8             return""; 9}Else{  Ten             returnS.trim ();  One         }   A}

V: Used to remove the list of empty values and the same items.  

1     /** 2 * Used to remove the list of empty values and the same items. 3      * @paramList4      * @return 5      */  6      PublicList<string> Removesameitem (list<string>list) {  7List<string> diflist =NewArraylist<string>(); 8          for(String t:list) {9             if(t! =NULL&&!diflist.contains (t)) {  Ten Diflist.add (t);  One             }   A         }   -         returndiflist;  -}

Six: The current days plus one day  

1      /** 2 * Number of days plus one3      * @paramDateTime4      * @return 5      * @throwsException6      */  7      Public StaticString Datefamte (String dateTime)throwsexception{8SimpleDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd"); 9Date Date =Format.parse (dateTime); TenCalendar calendar = Calendar.getinstance ();//Calendar Object OneCalendar.settime (date);//set the current date ACalendar.add (Calendar.day_of_month, 1);//days plus One -         returnFormat.format (Calendar.gettime ());  -}

Seven: Convert a JSON string to a Java object

1      /**     2 * Get an array of Java objects from a JSON array, such as:3 * [{"id": Idvalue, "name": Namevalue}, {"id": Idvalue, "name": Namevalue}, ...] 4      * @paramObject5      * @paramClazz6      * @return     7      */       8      Public Staticobject[] Getdtoarray (String jsonstring, Class clazz) {9Jsonarray array =Jsonarray.fromobject (jsonstring); Tenobject[] obj =Newobject[array.size ()];  One          for(inti = 0; I < array.size (); i++){         AJsonobject Jsonobject =Array.getjsonobject (i);  -Obj[i] =Jsonobject.tobean (Jsonobject, clazz);  -         }         the         returnobj; -}

Do the method in the tool class commonly used in the Java EE Project!

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.