package com.sclh.common;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
/**
* @author fhwei 2007-3-18 上午11:52:02
*/
public class ChineseString {
public ChineseString() {
}
// 中文字符串转换
public static String chineseStr(String str) {
try {
if (str == null)
return "";
String tempStr = str;
byte[] tempArray = tempStr.getBytes("ISO8859-1");
String temp = new String(tempArray);
return temp;
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/chineseStr() "
+ ex.getMessage());
}
return "";
}
// 中文字符串转换
public static String chineseStr2(String str) {
try {
if (str == null)
return "";
String tempStr = str;
byte[] tempArray = tempStr.getBytes("ISO8859-1");
String temp = new String(tempArray, "GBK");
return temp;
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/chineseStr2() "
+ ex.getMessage());
}
return "";
}
// 中文字符串转换,用于 js 中 encodeURI() 方式提交的参数转换
public static String chineseStr3(String str) {
try {
if (str == null)
return "";
String tempStr = str;
byte[] tempArray = tempStr.getBytes("ISO8859-1");
String temp = new String(tempArray, "UTF8");
return temp;
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/chineseStr3() "
+ ex.getMessage());
}
return "";
}
// URL中的中文字符串转换
public static String URLEncode(String str) {
String returnValue = "";
try {
if (str != null) {
returnValue = java.net.URLEncoder.encode(str, "GBK");
}
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/URLEncode() "
+ ex.getMessage());
}
return returnValue;
}
// URL中的中文字符串还原,没什么用还是乱码,用chineseStr()方法
public static String URLDecode(String str) {
String returnValue = "";
try {
if (str != null) {
returnValue = java.net.URLDecoder.decode(str, "GBK");
}
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/URLDecode() "
+ ex.getMessage());
}
return returnValue;
}
// Null字符串转换
public static String nullToStr(String temp) {
if (temp == null)
temp = "";
return temp;
}
// 取得字符串长度(一个汉字为两个字符长度)
public static long getStringLength(String sourceStr) {
long returnValue = 0;
if (sourceStr == null) {
return (returnValue);
}
for (int i = 0; i < sourceStr.length(); i++) {
char[] tempChar = sourceStr.substring(i, i + 1).toCharArray();
if ((int) tempChar[0] > 255)
returnValue += 2;
else
returnValue++;
}
return (returnValue);
}
// 取得重复次数的字符串
public static String getRepeatString(String sourceStr, long repeatTimes) {
StringBuffer returnStr = new StringBuffer();
for (int i = 0; i < repeatTimes; i++) {
returnStr.append(sourceStr);
}
return (returnStr.toString());
}
// 取得指定长度的字符串,不足长度的以replaceString填充(一个汉字为两个字符长度)
public static String getSpecifyLengthString(String sourceStr,
long specityLength, String replaceString) {
if (sourceStr == null) {
return (getRepeatString(replaceString, specityLength));
}
long realLength = getStringLength(sourceStr);
StringBuffer returnStr = new StringBuffer();
if (realLength < specityLength) {
returnStr.append(sourceStr);
returnStr.append(getRepeatString(replaceString, specityLength
- realLength));
} else {
returnStr.append(getLeftString(sourceStr, specityLength,
replaceString));
}
return (returnStr.toString());
}
// 取得其中包含字符串中从左边算起指定数量的字符(一个汉字为两个字符长度)
public static String getLeftString(String sourceStr, long leftLength,
String replaceString) {
StringBuffer returnStr = new StringBuffer();
long tempLength = 0;
for (int i = 0; i < sourceStr.length(); i++) {
String tempStr = sourceStr.substring(i, i + 1);
char[] tempChar = tempStr.toCharArray();
if ((int) tempChar[0] > 255)
tempLength += 2;
else
tempLength++;
if (tempLength >= leftLength) {
if (tempLength == leftLength)
returnStr.append(tempStr);
else
returnStr.append(getRepeatString(replaceString, tempLength
- leftLength));
break;
}
returnStr.append(tempStr);
}
return (returnStr.toString());
}
// 取得指定长度的字符串(一个汉字为两个字符长度,targetlength为汉字个数)
public static String displayTitle(String sourceString, long targetlength) {
String returnValue = "";
if (sourceString != null) {
if (getStringLength(sourceString) <= targetlength * 2)
returnValue = sourceString;
else
returnValue = getLeftString(sourceString,
(targetlength - 1) * 2, "")
+ "…";
}
return (returnValue);
}
// 取得一个日期的(2004-12-24)
public static String formatDate(Date convertDate) {
String returnValue = "";
if (convertDate != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
returnValue = formatter.format(convertDate);
}
return (returnValue);
}
// 取得一个日期的全名(2004-12-24 12:04:15)
public static String fullDate(Date convertDate) {
String returnValue = "";
if (convertDate != null) {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy年MM月dd日 HH:mm:ss");
returnValue = formatter.format(convertDate);
}
return (returnValue);
}
// 取得一个日期的中文名称(2004年12月24日)
public static String chineseDate(Date convertDate) {
String returnValue = "";
if (convertDate != null) {
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
returnValue = dateFormat.format(convertDate);
}
return (returnValue);
}
// 取得一个日期的中文名称,包括星期(2004年12月24日 星期五)
public static String chineseFullDate(Date convertDate) {
String returnValue = "";
if (convertDate != null) {
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
returnValue = dateFormat.format(convertDate);
}
return (returnValue);
}
// 取得一个日期字符的中文样式(二○○四年十二月二十四日)
public static String chineseStyleDate(String dateString) {
String returnValue = "";
String[] chinaNumber = new String[10];
chinaNumber[0] = "○";
chinaNumber[1] = "一";
chinaNumber[2] = "二";
chinaNumber[3] = "三";
chinaNumber[4] = "四";
chinaNumber[5] = "五";
chinaNumber[6] = "六";
chinaNumber[7] = "七";
chinaNumber[8] = "八";
chinaNumber[9] = "九";
String tempString = "";
try {
tempString = dateString.substring(0, 4);
for (int i = 0; i < tempString.length(); i++) {
int cn = Integer.parseInt(tempString.substring(i, i + 1));
returnValue += chinaNumber[cn];
}
returnValue += "年";
tempString = dateString.substring(5, 7);
for (int i = 0; i < tempString.length(); i++) {
int cn = Integer.parseInt(tempString.substring(i, i + 1));
if (i == 0) {
if (cn == 1) {
returnValue += "十";
}
} else
returnValue += chinaNumber[cn];
}
returnValue += "月";
tempString = dateString.substring(8);
for (int i = 0; i < tempString.length(); i++) {
int cn = Integer.parseInt(tempString.substring(i, i + 1));
if (i == 0) {
switch (cn) {
case 0:
break;
case 1:
returnValue += "十";
break;
default:
returnValue += chinaNumber[cn] + "十";
break;
}
} else
returnValue += chinaNumber[cn];
}
returnValue += "日";
} catch (Exception ex) {
returnValue = "";
System.out
.println("Exception : com.sclh.common.ChineseString/chineseStyleDate() "
+ ex.getMessage());
}
return (returnValue);
}
// 取得两个日期之间的时差(date1大于date2时, 切除小数;date1小于date2时,小数进位)
// 返回值(数组[0]= 1当date1>=date2 =2当date1<date2; 数组[1]=时间差距; 数组[2]=单位)
public static String[] getTimeInterval(Date date1, Date date2) {
String[] returnValue = new String[3];
long minute = (long) Math
.floor((date1.getTime() - date2.getTime()) / 60000);
if (minute >= 0)
returnValue[0] = "1";
else
returnValue[0] = "2";
if (Math.abs(minute) >= 60) {
long hour = (long) Math.floor(minute / 60.0);
if (Math.abs(hour) >= 24) {
long day = (long) Math.floor(hour / 24.0);
returnValue[1] = "" + Math.abs(day);
returnValue[2] = "天";
} else {
returnValue[1] = "" + Math.abs(hour);
returnValue[2] = "小时";
}
} else {
returnValue[1] = "" + Math.abs(minute);
returnValue[2] = "分";
}
return (returnValue);
}
// 返回一个字符串数组,str被splitStr分割后的结果
public static String[] split(String str, String splitStr) {
Collection coll = new ArrayList();
if (str != null && !"".equals(str) && splitStr != null
&& !"".equals(splitStr)) {
String tempStr = str;
int at = tempStr.indexOf(splitStr);
while (at != -1) {
coll.add(tempStr.substring(0, at));
tempStr = tempStr.substring(at + splitStr.length());
at = tempStr.indexOf(splitStr);
}
if (!"".equals(tempStr))
coll.add(tempStr);
}
return (String[]) coll.toArray(new String[0]);
}
// 当前字符转换成HTML格式
public static String htmlFormat(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll("/n", "<br>");
returnValue = returnValue.replaceAll(" ", " ");
}
return (returnValue);
}
// 当前字符的换行符转换成空格
public static String newlineFormat(String str) {
String returnValue = "";
String enterNewline = String.valueOf((char) 13)
+ String.valueOf((char) 10);
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll(enterNewline, " ");
}
return (returnValue);
}
// 当前字符的回车换行符转换成回车符
public static String enterFormat(String str) {
String returnValue = "";
String enterNewline = String.valueOf((char) 13)
+ String.valueOf((char) 10);
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll(enterNewline, String
.valueOf((char) 13));
}
return (returnValue);
}
// 字符串转换成日期 formatString可以是 yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd
public static java.util.Date stringToDate(String dateString,
String formatString) {
java.util.Date returnValue = null;
try {
if (dateString != null && !"".equals(dateString)) {
SimpleDateFormat formatter = new SimpleDateFormat(formatString);
returnValue = formatter.parse(dateString);
}
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/stringToDate() "
+ ex.getMessage());
}
return (returnValue);
}
// 获得List列表中排序的标识,用户点击排序字段时用
public static String getOrderFlag(String orderByField, String orderName) {
String returnValue = "";
if (orderByField != null && orderByField.indexOf(orderName) != -1)
if (orderByField.equals(orderName))
returnValue = "↑";
else
returnValue = "↓";
return returnValue;
}
/**
* 取得一个日期的字符表达式,格式如: (20041224120626)
*
* @param convertDate
* 需要转换的日期
* @return String
*/
public static String fullDateCompact(Date convertDate) {
String returnValue = "";
if (convertDate != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
returnValue = formatter.format(convertDate);
}
return (returnValue);
}
/**
* Description: 2007-3-18上午11:52:02
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
/**
* @author fhwei 2007-3-18 上午11:52:02
*/
public class ChineseString {
public ChineseString() {
}
// 中文字符串转换
public static String chineseStr(String str) {
try {
if (str == null)
return "";
String tempStr = str;
byte[] tempArray = tempStr.getBytes("ISO8859-1");
String temp = new String(tempArray);
return temp;
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/chineseStr() "
+ ex.getMessage());
}
return "";
}
// 中文字符串转换
public static String chineseStr2(String str) {
try {
if (str == null)
return "";
String tempStr = str;
byte[] tempArray = tempStr.getBytes("ISO8859-1");
String temp = new String(tempArray, "GBK");
return temp;
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/chineseStr2() "
+ ex.getMessage());
}
return "";
}
// 中文字符串转换,用于 js 中 encodeURI() 方式提交的参数转换
public static String chineseStr3(String str) {
try {
if (str == null)
return "";
String tempStr = str;
byte[] tempArray = tempStr.getBytes("ISO8859-1");
String temp = new String(tempArray, "UTF8");
return temp;
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/chineseStr3() "
+ ex.getMessage());
}
return "";
}
// URL中的中文字符串转换
public static String URLEncode(String str) {
String returnValue = "";
try {
if (str != null) {
returnValue = java.net.URLEncoder.encode(str, "GBK");
}
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/URLEncode() "
+ ex.getMessage());
}
return returnValue;
}
// URL中的中文字符串还原,没什么用还是乱码,用chineseStr()方法
public static String URLDecode(String str) {
String returnValue = "";
try {
if (str != null) {
returnValue = java.net.URLDecoder.decode(str, "GBK");
}
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/URLDecode() "
+ ex.getMessage());
}
return returnValue;
}
// Null字符串转换
public static String nullToStr(String temp) {
if (temp == null)
temp = "";
return temp;
}
// 取得字符串长度(一个汉字为两个字符长度)
public static long getStringLength(String sourceStr) {
long returnValue = 0;
if (sourceStr == null) {
return (returnValue);
}
for (int i = 0; i < sourceStr.length(); i++) {
char[] tempChar = sourceStr.substring(i, i + 1).toCharArray();
if ((int) tempChar[0] > 255)
returnValue += 2;
else
returnValue++;
}
return (returnValue);
}
// 取得重复次数的字符串
public static String getRepeatString(String sourceStr, long repeatTimes) {
StringBuffer returnStr = new StringBuffer();
for (int i = 0; i < repeatTimes; i++) {
returnStr.append(sourceStr);
}
return (returnStr.toString());
}
// 取得指定长度的字符串,不足长度的以replaceString填充(一个汉字为两个字符长度)
public static String getSpecifyLengthString(String sourceStr,
long specityLength, String replaceString) {
if (sourceStr == null) {
return (getRepeatString(replaceString, specityLength));
}
long realLength = getStringLength(sourceStr);
StringBuffer returnStr = new StringBuffer();
if (realLength < specityLength) {
returnStr.append(sourceStr);
returnStr.append(getRepeatString(replaceString, specityLength
- realLength));
} else {
returnStr.append(getLeftString(sourceStr, specityLength,
replaceString));
}
return (returnStr.toString());
}
// 取得其中包含字符串中从左边算起指定数量的字符(一个汉字为两个字符长度)
public static String getLeftString(String sourceStr, long leftLength,
String replaceString) {
StringBuffer returnStr = new StringBuffer();
long tempLength = 0;
for (int i = 0; i < sourceStr.length(); i++) {
String tempStr = sourceStr.substring(i, i + 1);
char[] tempChar = tempStr.toCharArray();
if ((int) tempChar[0] > 255)
tempLength += 2;
else
tempLength++;
if (tempLength >= leftLength) {
if (tempLength == leftLength)
returnStr.append(tempStr);
else
returnStr.append(getRepeatString(replaceString, tempLength
- leftLength));
break;
}
returnStr.append(tempStr);
}
return (returnStr.toString());
}
// 取得指定长度的字符串(一个汉字为两个字符长度,targetlength为汉字个数)
public static String displayTitle(String sourceString, long targetlength) {
String returnValue = "";
if (sourceString != null) {
if (getStringLength(sourceString) <= targetlength * 2)
returnValue = sourceString;
else
returnValue = getLeftString(sourceString,
(targetlength - 1) * 2, "")
+ "…";
}
return (returnValue);
}
// 取得一个日期的(2004-12-24)
public static String formatDate(Date convertDate) {
String returnValue = "";
if (convertDate != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
returnValue = formatter.format(convertDate);
}
return (returnValue);
}
// 取得一个日期的全名(2004-12-24 12:04:15)
public static String fullDate(Date convertDate) {
String returnValue = "";
if (convertDate != null) {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy年MM月dd日 HH:mm:ss");
returnValue = formatter.format(convertDate);
}
return (returnValue);
}
// 取得一个日期的中文名称(2004年12月24日)
public static String chineseDate(Date convertDate) {
String returnValue = "";
if (convertDate != null) {
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
returnValue = dateFormat.format(convertDate);
}
return (returnValue);
}
// 取得一个日期的中文名称,包括星期(2004年12月24日 星期五)
public static String chineseFullDate(Date convertDate) {
String returnValue = "";
if (convertDate != null) {
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
returnValue = dateFormat.format(convertDate);
}
return (returnValue);
}
// 取得一个日期字符的中文样式(二○○四年十二月二十四日)
public static String chineseStyleDate(String dateString) {
String returnValue = "";
String[] chinaNumber = new String[10];
chinaNumber[0] = "○";
chinaNumber[1] = "一";
chinaNumber[2] = "二";
chinaNumber[3] = "三";
chinaNumber[4] = "四";
chinaNumber[5] = "五";
chinaNumber[6] = "六";
chinaNumber[7] = "七";
chinaNumber[8] = "八";
chinaNumber[9] = "九";
String tempString = "";
try {
tempString = dateString.substring(0, 4);
for (int i = 0; i < tempString.length(); i++) {
int cn = Integer.parseInt(tempString.substring(i, i + 1));
returnValue += chinaNumber[cn];
}
returnValue += "年";
tempString = dateString.substring(5, 7);
for (int i = 0; i < tempString.length(); i++) {
int cn = Integer.parseInt(tempString.substring(i, i + 1));
if (i == 0) {
if (cn == 1) {
returnValue += "十";
}
} else
returnValue += chinaNumber[cn];
}
returnValue += "月";
tempString = dateString.substring(8);
for (int i = 0; i < tempString.length(); i++) {
int cn = Integer.parseInt(tempString.substring(i, i + 1));
if (i == 0) {
switch (cn) {
case 0:
break;
case 1:
returnValue += "十";
break;
default:
returnValue += chinaNumber[cn] + "十";
break;
}
} else
returnValue += chinaNumber[cn];
}
returnValue += "日";
} catch (Exception ex) {
returnValue = "";
System.out
.println("Exception : com.sclh.common.ChineseString/chineseStyleDate() "
+ ex.getMessage());
}
return (returnValue);
}
// 取得两个日期之间的时差(date1大于date2时, 切除小数;date1小于date2时,小数进位)
// 返回值(数组[0]= 1当date1>=date2 =2当date1<date2; 数组[1]=时间差距; 数组[2]=单位)
public static String[] getTimeInterval(Date date1, Date date2) {
String[] returnValue = new String[3];
long minute = (long) Math
.floor((date1.getTime() - date2.getTime()) / 60000);
if (minute >= 0)
returnValue[0] = "1";
else
returnValue[0] = "2";
if (Math.abs(minute) >= 60) {
long hour = (long) Math.floor(minute / 60.0);
if (Math.abs(hour) >= 24) {
long day = (long) Math.floor(hour / 24.0);
returnValue[1] = "" + Math.abs(day);
returnValue[2] = "天";
} else {
returnValue[1] = "" + Math.abs(hour);
returnValue[2] = "小时";
}
} else {
returnValue[1] = "" + Math.abs(minute);
returnValue[2] = "分";
}
return (returnValue);
}
// 返回一个字符串数组,str被splitStr分割后的结果
public static String[] split(String str, String splitStr) {
Collection coll = new ArrayList();
if (str != null && !"".equals(str) && splitStr != null
&& !"".equals(splitStr)) {
String tempStr = str;
int at = tempStr.indexOf(splitStr);
while (at != -1) {
coll.add(tempStr.substring(0, at));
tempStr = tempStr.substring(at + splitStr.length());
at = tempStr.indexOf(splitStr);
}
if (!"".equals(tempStr))
coll.add(tempStr);
}
return (String[]) coll.toArray(new String[0]);
}
// 当前字符转换成HTML格式
public static String htmlFormat(String str) {
String returnValue = "";
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll("/n", "<br>");
returnValue = returnValue.replaceAll(" ", " ");
}
return (returnValue);
}
// 当前字符的换行符转换成空格
public static String newlineFormat(String str) {
String returnValue = "";
String enterNewline = String.valueOf((char) 13)
+ String.valueOf((char) 10);
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll(enterNewline, " ");
}
return (returnValue);
}
// 当前字符的回车换行符转换成回车符
public static String enterFormat(String str) {
String returnValue = "";
String enterNewline = String.valueOf((char) 13)
+ String.valueOf((char) 10);
if (str != null && !"".equals(str)) {
returnValue = str.replaceAll(enterNewline, String
.valueOf((char) 13));
}
return (returnValue);
}
// 字符串转换成日期 formatString可以是 yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd
public static java.util.Date stringToDate(String dateString,
String formatString) {
java.util.Date returnValue = null;
try {
if (dateString != null && !"".equals(dateString)) {
SimpleDateFormat formatter = new SimpleDateFormat(formatString);
returnValue = formatter.parse(dateString);
}
} catch (Exception ex) {
System.out
.println("Exception : com.sclh.common.ChineseString/stringToDate() "
+ ex.getMessage());
}
return (returnValue);
}
// 获得List列表中排序的标识,用户点击排序字段时用
public static String getOrderFlag(String orderByField, String orderName) {
String returnValue = "";
if (orderByField != null && orderByField.indexOf(orderName) != -1)
if (orderByField.equals(orderName))
returnValue = "↑";
else
returnValue = "↓";
return returnValue;
}
/**
* 取得一个日期的字符表达式,格式如: (20041224120626)
*
* @param convertDate
* 需要转换的日期
* @return String
*/
public static String fullDateCompact(Date convertDate) {
String returnValue = "";
if (convertDate != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
returnValue = formatter.format(convertDate);
}
return (returnValue);
}
/**
* Description: 2007-3-18上午11:52:02
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}