標籤:
公司需求年年有,今年有點小特殊,哈哈。
忽然加了個需求,說要實現漢字轉拼音查詢。
在努力下寫出來了,現在分享一下吧!!!
/** * 漢字轉拼音縮寫 * * @param str * 要轉換的漢字字串 * @return String 拼音縮寫 */public static String getPYString(String str) {String tempStr = "";for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);if (c >= 33 && c <= 126) {// 字母和符號原樣保留tempStr += String.valueOf(c);} else {// 累加拼音聲母tempStr += getPYChar(String.valueOf(c));}}if (tempStr.length() > 1) {tempStr = tempStr.substring(0, 1);}return tempStr;}/** * 取單個字元的拼音聲母 * * @param c * //要轉換的單個漢字 * @return String 拼音聲母 */public static String getPYChar(String c) {byte[] array = new byte[2];array = String.valueOf(c).getBytes();int i = (short) (array[0] - ‘\0‘ + 256) * 256+ ((short) (array[1] - ‘\0‘ + 256));if (i < 0xB0A1)return "*";if (i < 0xB0C5)return "a";if (i < 0xB2C1)return "b";if (i < 0xB4EE)return "c";if (i < 0xB6EA)return "d";if (i < 0xB7A2)return "e";if (i < 0xB8C1)return "f";if (i < 0xB9FE)return "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)return "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 >
另外在做程式的時候,很多時候都是需要做時間字元轉譯,填充下
文章的內容吧。現在給出對應的時間字元的轉換!!!
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm";public static final String DATE_FORMAT2 = "yyyy-MM-dd HH:mm:ss";public static final String YEAR_MONTH_FORMAT = "yyyy-MM";public static final String DATE_FORMAT3 = "HH:mm:ss";public static final String DATE_FORMAT1 = "yyyyMMddHHmmss"; /** * 時間格式,採用24小時制 */public static final String TIME_FORMAT = "HH:mm";/** * 不帶秒的時間日期格式 */public static final String DATE_FORMAT_NO_SECOND = "yyyy-MM-dd HH:mm";/** * 不是時間的日期格式 */public static final String DATE_FORMAT_NO_TIME = "yyyy-MM-dd"; // 返回對應的時間字串public static String getStrByDate(Date date) {String str = "";SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);str = sdf.format(date);return str;}// 返回對應的時間字串public static String getStrByDate2(Date date) {String str = "";SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);str = sdf.format(date);return str;}// 返回對應的時間字串public static String getStrByDate1(Date date) {String str = "";SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT2);str = sdf.format(date);return str;}/** * 將日期轉換成字串格式 * * @param date * @return */public static String dateTran2Str(Date date) {SimpleDateFormat dateFormat = new SimpleDateFormat(StrUtil.DATE_FORMAT_NO_SECOND);return dateFormat.format(date);}/** * 將日期轉換成字串格式 * * @param date * @return */public static String BillRandom(Date date) {SimpleDateFormat dateFormat = new SimpleDateFormat(StrUtil.DATE_FORMAT1);return dateFormat.format(date);}/** * 將日期轉換成字串格式 * * @param date * @param format日期格式 * @return */public static String dateTran2Str(Date date, String format) {SimpleDateFormat dateFormat = new SimpleDateFormat(format);return dateFormat.format(date);}/** * 將日期轉換成字串格式 * * @param date * @param format日期格式 * @return */public static String dateTran2Str(Object date, String format) {SimpleDateFormat dateFormat = new SimpleDateFormat(format);return dateFormat.format(date);}/** * 將日期轉換成字串格式 * * @param date * @param format日期格式 * @return */public static String dateTran2Str(Object date) {SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_NO_SECOND);return dateFormat.format(date);}/** * 帶秒的時間格式 * * @param date * @param format日期格式 * @return */public static String dateTranStr2exc(Object date) {SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT2);return dateFormat.format(date);}/** * 將日期型字串轉換為日期 * * @param dateStr * 一定要是日期轉過來的日期型字串,否則會有ParseException * @return null表示ParseException */public 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;}/** * 將日期型字串轉換為日期 * * @param dateStr * 一定要是日期轉過來的日期型字串,否則會有ParseException * @param pattern指定的時間格式 * @return null表示ParseException */public static Date strTran2Date(String dateStr, String pattern) {try {return new SimpleDateFormat(pattern).parse(dateStr);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}/** * float型資料轉換成String * * @param f * @return */public static String floatTran2Str(float f) {return String.valueOf(f);}/** * 將時間轉換成long型時間戳記 * * @param timeStr * 時間的字串表示,格式為:HH:mm * @return */public static Long strTran2Time(String timeStr) {SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);Date time = null;try {int len = timeStr.length();if (timeStr.length() > 10) {timeStr = timeStr.substring(10);}time = timeFormat.parse(timeStr);return time.getTime();} catch (ParseException e) {System.out.println(new Exception("日期格式非法!").getMessage());}return null;}/** * 將資料庫的時間轉換成字串 * * @param t時間 * @return */public static String timeTran2Str(Object t) {SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);String timeStr = null;timeStr = timeFormat.format(t);return timeStr;}/** * 將時間戳記轉換成字串 * * @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;}
最後,我們經常要做一些字元為空白判斷之類的,和其他資料的正則
運算式的驗證,現在也給出福利吧!!!
/** * 判斷輸入的字串參數是否為空白 * * @return boolean 空則返回true,非空則flase */public static boolean isEmpty(String input) {return null == input || 0 == input.length()|| 0 == input.replaceAll("\\s", "").length();}/** * 判斷輸入的位元組數組是否為空白 * * @return boolean 空則返回true,非空則flase */public static boolean isEmpty(byte[] bytes) {return null == bytes || 0 == bytes.length;}/** * 位元組數組轉為字串 * * @see 該方法預設以ISO-8859-1轉碼 * @see 若想自己指定字元集,可以使用<code>getString(byte[] data, String charset)</code>方法 */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;}// 匹配輸入資料類型public static boolean matchCheck(String ins, int type) {String pat = "";switch (type) {case 0: // /手機號pat = "^1[3-8][0-9]{9}$";break;case 1:// /郵箱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: // /使用者名稱pat = "^[0-9a-zA-Z]{4,12}$";break;case 3: // /密碼pat = "^[\\s\\S]{6,18}$";break;case 4: // /中文pat = "^[0-9a-z\u4e00-\u9fa5|admin]{2,15}$";break;case 5: // /非零正整數pat = "^\\+?[1-9][0-9]*$";break;case 6: // /數字和字母pat = "^[A-Za-z0-9]+$";break;case 7: // /1-9的數字pat = "^[1-9]";break;case 8: // /身份證pat = "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";break;case 9: // /名字pat = "^([A-Za-z]|[\u4E00-\u9FA5])+$";break;case 10: // /時間 時:分:秒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字元操作擷取漢字的拼音以及其他常用工具