/** * 汉字转拼音缩写 * * @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 block e.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 block e.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; }

 

 

最后,我们常常要做一些字符为空推断之类的,和其它数据的正则

表达式的验证,如今也给出福利吧。。。

 

 


公司需求年年有,今年有点小特殊,哈哈。

忽然加了个需求,说要实现汉字转拼音查询。

在努力下写出来了,如今分享一下吧!。!

 

 

/** * 汉字转拼音缩写 * * @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 block e.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 block e.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; }

 

 

最后,我们常常要做一些字符为空推断之类的,和其它数据的正则

表达式的验证,如今也给出福利吧。。。