Java character manipulation gets the pinyin of Chinese characters and other frequently used tools

Source: Internet
Author: User
Tags array to string dateformat string format

Company needs every year, this year a little special, haha.

Suddenly added a demand, said to achieve the Chinese character to pinyin query.

In the effort to write out, now share it!!

/** * Kanji Pinyin abbreviation * * @param str * character string to convert * @return string phonetic abbreviation */public static string getpystring (String str) {String tempstr = ""; for (int i = 0; I < str.length (); i++) {char c = str.charat (i); if (c >= && C <= 126) {//letters and symbols remain as-is tempstr + = string.valueof (c);} else {//tired Add Pinyin initials TEMPSTR + = Getpychar (string.valueof (c));}} if (Tempstr.length () > 1) {tempstr = tempstr.substring (0, 1);} return TEMPSTR;} /** * Pinyin initials for single characters * * @param c *//single kanji to convert * @return String pinyin initials */public static string Getpychar (string c) {b yte[] Array = new Byte[2];array = String.valueof (c). GetBytes (); int i = (short) (Array[0]-' 256+ ' + n ') * ((short) (AR RAY[1]-' + + '), if (I < 0XB0A1) return "*", if (I < 0XB0C5) return "a", if (I < 0xb2c1) return "B"; if (I < 0x B4EE) return "C", if (I < 0XB6EA) return "D", if (I < 0XB7A2) return "E", if (I < 0XB8C1) return "F"; if (I < 0XB9FE) R Eturn "G"; if (I < 0XBBF7) return "H"; if (I < 0XBFA6) return "J"; if (I < 0xC0AC) return "K", if (I < 0xc2e8) return "L", if (I < 0XC4C3) return "M", if (I < 0XC5B6) return "n"; if (I < 0xc5be) re Turn "O", if (I < 0XC6DA) return "P", if (I < 0XC8BB) return "Q", if (I < 0XC8F6) return "R", if (I < 0xCBFA) return " S "; if (I < 0xCDDA) return" T ", if (I < 0XCEF4) return" W ", if (I < 0XD1B9) return" X ", if (I < 0xd4d1) return" Y "; if (I < 0XD7FA) return "Z"; return "*";} </textarea >

In addition, in the process of doing, very often need to do time character translation. Fill under

The content of the article. Now give the corresponding time character conversion.

public static final String date_format = "Yyyy-mm-dd hh:mm";p ublic static final String date_format2 = "Yyyy-mm-dd HH:mm:ss ";p ublic static final String year_month_format =" yyyy-mm ";p ublic static final String date_format3 =" HH:mm:ss ";p u Blic static final String date_format1 = "YYYYMMDDHHMMSS"; /** * Time format with 24-hour */public static final string time_format = "hh:mm";/** * time date format without seconds */public static final string date_f Ormat_no_second = "Yyyy-mm-dd hh:mm";/** * is not the date format of the time */public static final String date_format_no_time = "YYYY-MM-DD"; Returns the corresponding time string public static string Getstrbydate (date date) {string str = ""; SimpleDateFormat SDF = new SimpleDateFormat (date_format); str = Sdf.format (DATE); return str;} Returns the corresponding time string public static string GetStrByDate2 (date date) {string str = ""; SimpleDateFormat SDF = new SimpleDateFormat (date_format); str = Sdf.format (DATE); return str;} Returns the corresponding time string public static string getStrByDate1 (date date) {string str = ""; SimpleDateFormat SDF = new SimpleDateFormat (DATE_FORMAT2); str = Sdf.format (DATE); return str;} /** * Convert Date to string format * * @param date * @return */public static string datetran2str (date date) {SimpleDateFormat DateFormat = New SimpleDateFormat (Strutil.date_format_no_second); return Dateformat.format (DATE);} /** * Convert Date to string format * * @param date * @return */public static string billrandom (date date) {SimpleDateFormat DateFormat = n EW SimpleDateFormat (STRUTIL.DATE_FORMAT1); return Dateformat.format (DATE);} /** * Convert Date to string format * * @param date * @param format date format * @return */public static string datetran2str (date date, string for MAT) {SimpleDateFormat DateFormat = new SimpleDateFormat (format); return Dateformat.format (date);} /** * Convert Date to string format * * @param date * @param format date format * @return */public static string Datetran2str (Object date, string f ormat) {SimpleDateFormat DateFormat = new SimpleDateFormat (format); return Dateformat.format (date);} /** * Convert Date to string format * * @param date * @param format date format * @return */public static string dateTran2STR (Object date) {SimpleDateFormat DateFormat = new SimpleDateFormat (date_format_no_second); return Dateformat.format ( date);} /** * Time format with seconds * * @param date * @param format date format * @return */public static String datetranstr2exc (Object date) {Simpleda Teformat DateFormat = new SimpleDateFormat (DATE_FORMAT2); return Dateformat.format (DATE);} /** * Converts a date string to a date * * @param datestr * Must be a date string that is turned around, otherwise there will be parseexception * @return null represents parseexception */p Ublic static Date strtran2date (String datestr) {try {return new SimpleDateFormat (Strutil.date_format_no_second). Parse ( DATESTR);} catch (ParseException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;} /** * Converts a date string to a date * * @param datestr * Must be the date string that the date is turned over. Otherwise there will be parseexception * @param pattern Specifies the time format * @return null represents parseexception */public static Date strtran2date (String datest R, String pattern) {try {return new SimpleDateFormat (pattern). Parse (DATESTR);} catch (ParseException e) {//TODO Auto-gene Rated CATch blocke.printstacktrace ();} return null;} /** * FLOAT data into String * * @param f * @return */public static String floattran2str (float f) {return string.valueof (f);} /** * Convert time to long timestamp * * @param timestr * Time string representation in the format: hh:mm * @return */public static long Strtran2time (Strin G timestr) {SimpleDateFormat TimeFormat = new SimpleDateFormat (Time_format);D ate time = null;try {int len = Timestr.length (); if (Timestr.length () >) {timestr = timestr.substring (10);} Time = Timeformat.parse (TIMESTR); return Time.gettime ();} catch (ParseException e) {System.out.println (New Exception ("date format is illegal! </p><p> "). GetMessage ());} return null;} /** * Convert the time of the database to a string * * @param t time * @return */public static string timetran2str (Object t) {SimpleDateFormat TimeFormat = New SimpleDateFormat (Time_format); String timestr = Null;timestr = Timeformat.format (t); return TIMESTR;} /** * Convert Timestamp to String * * @param timestampstring * @return */public static string Timestamp2date (String timestampstring) {Long TiMestamp = Long.parselong (timestampstring) * 1000; String date = new Java.text.SimpleDateFormat ("Yyyy-mm-dd"). Format (new java.util.Date (timestamp)); return date;} public static string Strtranbirth (String day) {if (day.length () = = 8) {String y = day.substring (0, 4); String m = day.substring (4, 6); String d = day.substring (6, 8); return y + "-" + M + "-" + D;} Return day;}

Finally, we often have to do some of the characters for null inference and the like, and other data of the regular

The validation of expressions, now also gives benefits ...

/** * Infer if the input string parameter is empty * * @return Boolean NULL returns TRUE, non-null flase */public static Boolean isEmpty (string input) {return NULL = = Input | | 0 = = Input.length () | | 0 = = Input.replaceall ("\\s", ""). Length ();} /** * Infers if the input byte array is empty * * @return Boolean NULL returns TRUE, non-null flase */public static Boolean isEmpty (byte[] bytes) {return NULL = = b Ytes | | 0 = = Bytes.length;} /** * byte array to String * * @see This method by default iso-8859-1 transcoding * @see If you want to specify a character set, you can use <code>getstring (byte[] data, string charset) </c Ode> method */public static string getString (byte[] data) {string str = ""; try {str = new String (data, "iso-8859-1");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return str;} Match input data type public static Boolean Matchcheck (string ins, int type) {string Pat = ""; switch (type) {case 0:////Mobile number Pat = "^1 [3-8] [0-9] {9}$ "; break;case 1:///mailbox Pat =" ^[a-za-z0-9][\\w\\.-]*[a-za-z0-9]@[a-za-z0-9][\\w\\.-]*[a-za-z0-9]\\. [A-za-z] [A-za-z\\.] *[a-za-z]$ "; Break;case 2:///usernamepat =" ^[0-9a-za-z]{4,12}$ "; break;case 3:///passwordpat = "^[\\s\\s]{6,18}$"; Break;case 4:///Chinese Pat = "^[0-9a-z\u4e00-\u9fa5|admin]{2,15}$"; Break;case 5:// /non-0 positive integer pat = "^\\+?" [1-9] [0-9]*$]; Break;case 6:///number and letter Pat = "^[a-za-z0-9]+$"; Break;case 7:///1-9 Digital Pat = "^[1-9]"; Break;case 8:///Identity card Pat = " ^ (\\d{15}$|^\\d{18}$|^\\d{17} (\\d| X|X)) $ "; Break;case 9:////Name Pat =" ^ ([a-za-z]|[ \U4E00-\U9FA5]) +$ "; Break;case 10:///Time: min: sec Pat =" ^ ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) $ "; break;} Pattern p = pattern.compile (PAT); Matcher m = p.matcher (INS); return m.matches ();}

Java character manipulation gets pinyin for Chinese characters and other frequently used tools

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.