Generic Tool class methods

Source: Internet
Author: User

Package com.ctb.utils; Import Java.sql.timestamp;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.calendar;import Java.util.date;import java.util.List; Import Net.sf.json.JSONArray; Import Org.apache.commons.codec.digest.digestutils;import Org.apache.commons.collections.collectionutils;import  Org.apache.commons.lang.arrayutils;import Org.apache.commons.lang.stringutils;import Org.apache.log4j.Logger;    public class Myutiltool {private static Logger Logger = Logger.getlogger (Myutiltool.class); /** * Date Format parameter * @author */public static class date_param{public static String standard_date = "y        Yyy-mm-dd HH:mm:ss ";        public static String standard_date_simple = "YYYY-MM-DD";        public static String chn_date = "yyyy mm month DD day hh" mm min ss sec ";    public static String chn_date_simple = "yyyy mm month dd Day";  /** * Date Processing tool * @author */public static class date{/** * Get current time * @reTurn */public static Long GetCurrentTime () {return (new Date ()). GetTime (); /** * Gets the formatting string for the current date * @param formatestr * @return * */public Stati C string Currentdatestring (String formatestr) {if (Stringutils.isempty (FORMATESTR)) {Formatest R = Myutiltool.date_param.            Standard_date;            } SimpleDateFormat SimpleDateFormat = new SimpleDateFormat (FORMATESTR);            try {return Simpledateformat.format (new Date ());            } catch (Exception e) {logger.error ("date formatted exception");        } return null; }/** * Date format * @param date * @return String */public static string Dateto String (Date date,string formatestr) {if (Stringutils.isempty (FORMATESTR)) {formatestr = Myutil Tool.date_param.            Standard_date; } SimpleDateFormat SimpleDateFormat = new SimpleDateFormat (FORMATESTR);            try {return Simpledateformat.format (date);            } catch (Exception e) {logger.error ("date formatted exception");        } return null; /** * String converted to date * * @param datestr * @return * */public static Dat E stringtodate (String datestr,string formatestr) {if (Stringutils.isempty (FORMATESTR)) {format Estr = Myutiltool.date_param.            Standard_date;                } if (datestr! = null && datestr.length () > 0) {datestr = Formatdatestr (DATESTR);                SimpleDateFormat SimpleDateFormat = new SimpleDateFormat (FORMATESTR);                try {return simpledateformat.parse (DATESTR);                } catch (ParseException e) {logger.error ("date formatted exception");        }} return null;                       }   /** * Format Date String * @param datestr * @return * */private static string Formatdatestr (String datestr)            {string[] STRs = Datestr.split ("-");            String str = "";                for (int i = 0; i < strs.length; i++) {String ele = strs[i];                    if (i>0) {if (Ele.length () ==1) {ele = "0" +ele;                } str=str+ "-" +ele;                }else {Str=ele;        }} return str;        /** * Convert SQL Date type to date type * * @param sqldate * @return * *            public static Date sqldatetodate (Java.sql.Date sqldate) {Long time = Sqldate.gettime ();            Date date = new Date (time);        return date; }/** * Convert util date type to SQL Date * * @return */public static Timestamp CU RRentdatetosqltimestamp () {Date date = new Date ();            Timestamp Timestamp = new Timestamp (Date.gettime ());        return timestamp; /** * Convert util date type to SQL date * @param date * @return */Public Static Timestamp Datetosqltimestamp (date date) {if (date!=null) {Timestamp Timestamp = new Time                Stamp (Date.gettime ());            return timestamp;        } return null; /** * Convert timestamp to date type * @param timestamp * @return Date * * Public St atic Date sqltimestamptodate (Timestamp Timestamp) {if (timestamp!=null) {Date date = new Date (t                Imestamp.gettime ());            return date;        } return null; /** * Compared to system time * @param ExpiredTime * @return Boolean */Public St atic Boolean checkexpiredtime (DateExpiredTime) {if (expiredtime! = null) {Date nowdate = new Date ();                if (Expiredtime.before (nowdate)) {return true;                } else {return false;        }} return false;        /** * Date Calculation * @param field * @param calcount * @return */ public static date calculatedate (date orgdate,int field,int calcount) {Calendar calendar = Calendar.getins            Tance ();            Calendar.settime (orgdate);            Calendar.add (field, Calcount);            Orgdate = Calendar.gettime ();        return orgdate;         /** * Set Modification date content * @param orgdate * @param field * @param setValue  * @return */public static date calculatesetdate (date orgdate,int field,int setValue) {Calendar            Calendar = Calendar.getinstance (); CalenDar.settime (orgdate);            Calendar.set (field, setValue);            Orgdate = Calendar.gettime ();        return orgdate;         }}/** * string processing tool * @author */public static class string{/** * Verify string is empty            * * @param str * @return */public static Boolean checkstrnull (String str) {            if (str! = null && Str.trim (). Length () > 0) {return false;            } else {return true;        }} private static final int max_generate_count = 99999;        private static int generatecount = 0;                public static synchronized String getuniquestring () {if (Generatecount > Max_generate_count)            Generatecount = 0;            String Uniquenumber = long.tostring (System.currenttimemillis ()) + integer.tostring (generatecount);            generatecount++;        return uniquenumber;                } /** * Convert Chinese in string to nicode code * @param str * @return */public static string Chinesetoun            Icode (String str) {string result= "";                for (int i = 0; i < str.length (); i++) {int chr1 = (char) str.charat (i); if (chr1>=19968&&chr1<=171941) {//Kanji range \u4e00-\u9fa5 (Chinese) result+= "\\u" + Integer.tohexstri                Ng (CHR1);                }else{Result+=str.charat (i);            }} try {result = new String (Result.getbytes (), "utf-8");            } catch (Exception e) {System.out.println ("Unsupported encoding format");        } return result; /** * Regular Validation of strings * @param str * @param reg * @return Boolean *            /public static Boolean Checkstrignreg (String str, String reg) {Boolean regcheck = Str.matches (reg);        return regcheck; }                /** * Replacement Character * @param str * @param oldChar * @param newchar * @retur n */public static string replace (string str,string oldchar,string Newchar) {if (stringutils.isn            Otempty (str)) {str = str.replace (OldChar, Newchar);        } return str; }/** * MD5 encrypted String * @param password * @return * */public static Strin            G Cipherpassword (String password) {string cipherpassword = Digestutils.md5hex (password);        return Cipherpassword; /** * Remove the space contained in the string * @param str * @return */public static string Removebl Anksinstring (String str) {if (str! = null && Str.trim (). Length () > 0) {str = Str.re            Place ("", "");        } return str; }/** * divides the string into * @param str * @paramSign * @return string[] */public static string[] Seperatestringtostrings (string str, string sign)                    {if (str! = null && Str.trim (). Length () > 0) {if (Str.contains (sign)) {                return Str.split (sign);        }} return null; /** * Divide the string into characters * @param orgstr * @param splitrange * @param sign * @ret URN */public static string Splitwithsign (string Orgstr,integer splitrange,string sign) {if (Str                Ingutils.isnotempty (ORGSTR)) {Integer length = orgstr.length ();                    if (length > Splitrange) {Integer splitnums = Length/splitrange;                    Integer lastmod = length% Splitrange;                    if (lastmod>0) {splitnums = splitnums+1; } string[] results = new string[Splitnums];                        for (int i = 0; i < splitnums; i++) {Integer startIndex = I*splitrange;                        Integer EndIndex = (i+1) *splitrange;                        if (endindex>length) {endIndex = length;                    } Results[i] = orgstr.substring (Startindex,endindex);                                         } String newstr = "";                    int length2 = Results.length;                            if (Arrayutils.isnotempty (results)) {for (int i = 0; i < length2; i++) {                            if (i==0) {newstr = results[i];                            }else {newstr = Newstr +sign+ results[i];     }}} return newstr;           }} return orgstr; }}/** * Collection class processing Tool * * @author */public static class COLLECTION {public static L                Ong[] Longslisttolongs (list<long> longlist) {if (Collectionutils.isnotempty (longlist)) {                long[] Longarray = new long[longlist.size ()];                for (int i = 0; i < longlist.size (); i++) {Longarray[i] = Longlist.get (i);            } return Longarray;        } return null; } public static object[] Objectslisttoobjects (list<object> objlist) {if (Collectionutils.isnotem                Pty (objlist)) {object[] array = new object[objlist.size ()];                Array = Objlist.toarray (array);            return array;        } return null; }} public static class Array{public static Boolean checkintinints (int[] results, int i) {i F (ARrayutils.isnotempty (Results)) {for (integer integer:results) {if (integer > i) {                    return true;        }}} return false; /** * Verify that the array contains content that is not empty * @param strings * @return * */public static Boolea n Checkstringsallnull (string...strings) {if (Arrayutils.isnotempty (strings)) {for (String Strin                    G:strings) {if (Stringutils.isnotempty (string)) {return true;        }}} return false; /** * Converts an array of type integer into an array of type long * @param orgarrays * @return */PU                Blic static long[] Integerstolongs (integer[] orgarrays) {if (Arrayutils.isnotempty (orgarrays)) {                Long[] results = new Long[orgarrays.length]; for (int i = 0; i< Orgarrays.length;                i++) {Results[i] = new Long (orgarrays[i]);            } return results;        } return null; /** * Converts an array of type string into an array of type long * @param orgarrays * @return */PU Blic static long[] Stringstolongs (string[] orgarrays) {if (Arrayutils.isnotempty (orgarrays)) {L                Ong[] results = new Long[orgarrays.length];                for (int i = 0; i < orgarrays.length; i++) {results[i] = new Long (orgarrays[i]);            } return results;        } return null;  /** * Convert an array of objects into a string * @param array * @return */public static string                Objectstostrings (object[] array,string sepsign) {if (array! = NULL && array.length > 0) {                String result = ""; for (int i = 0; i < array.length;i++) {if (i==0) {result = Array[0].tostring ();                    }else {result = Result+sepsign+array[i];            }} return result;        } return null;         }/** * Verifies that there is a string in the string array * @param strs * @param str * @return Boolean */public static Boolean checkstringinstrings (string[] strs, String str) {if (STRs! = null &&am P                        Strs.length > 0) {for (String string:strs) {if (String.Equals (str)) {                    return true;        }}} return false; /** * Object array to character array * @param objarray * @return */public static string[] Ob Jectstostrings (object[] objarray) {if (Arrayutils.isnotempty (Objarray)) {string[] StraRray = new String[objarray.length];                for (int i = 0; i < objarray.length; i++) {Strarray[i] = (String) objarray[i];            } return Strarray;        } return null; /** * Merges elements from a string array into a string */public static string Comb2str (string[] STRs, string            Sign) {String combstr = "";                    if (STRs! = null && strs.length > 0) {for (int i = 0; i < strs.length; i++) {                Combstr = Strs[i] + sign + combstr;        }} return combstr;         }} public static class json{/** * Convert Jsonarray string to sting array * @param jsonarraystr * @return */public static integer[] Jsonarraytointegerarray (String jsonarraystr) {if (Stringut      Ils.isnotempty (JSONARRAYSTR)) {Jsonarray Jsonarray = Jsonarray.fromobject (JSONARRAYSTR);          integer[] Array = new integer[jsonarray.size ()];                Array = (integer[]) Jsonarray.toarray (array);            return array;        } return null;        }} public static void Main (string[] args) {String value = MyUtilTool.DATE.currentDateString (null);    System.out.println (value); }}

  

Generic Tool class methods

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.