📄 commonutil.java
字号:
s += str.substring(0, pos) + "\\\\";
str = str.substring(pos + 1);
CommonUtil.printDebugInfo(str);
pos = str.indexOf("\\");
}
s += str;
return s;
// str.indexOf("\\")
}
/**
* translate a date string such as 2000-9-13 to a array of int
* @param String str the date string
* @return an array of int
*/
static public int[] getDateParts(String str) {
String[] adstr = split(str, '-');
int[] datePart = new int[6];
int i;
for (i = 0; i < datePart.length; i++) {
datePart[i] = Integer.parseInt(adstr[i]);
}
return datePart;
}
/**
* Sort a array of string
* @param String[] strs An array of String
* @return String[] the sorted array
*/
public static String[] sort(String[] strs) {
int i = 0, j = 1, len = strs.length;
if (len <= 1) {
return strs;
}
String strTmp = null;
for (i = 0; i < len - 1; i++) {
for (j = i + 1; j < len; j++) {
if (strs[i].compareTo(strs[j]) > 0) {
strTmp = strs[i];
strs[i] = strs[j];
strs[j] = strTmp;
}
}
}
return strs;
}
/**
* GET A date string with format "yyyy-mm-dd hh:mm:ss" of current time
*@return string For example "yyyy-mm-dd hh:mm:ss"
*/
static public String getCurrDateStr() {
java.util.Date date = new Date();
int year = date.getYear() + 1900;
int month = date.getMonth() + 1;
int day = date.getDate();
int hour = date.getHours();
int minute = date.getMinutes();
int second = date.getSeconds();
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" +
second;
}
/**
* translante an int to 人民币大写形式
* @param int money , money want to translate
* @return String[] the return array . 从"分" 到 "亿"
**/
public static String[] moneyToRMB(long money) {
String[] returnStr = {
"", "", "", "", "", "", "", "", "", ""};
int i, j, index;
long mod, result;
String[] RMB = {
"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
// for (i = 0; i < 10; i++) {
// try {
// byte[] byteTmp = RMB[i].getBytes("GB2312");
// RMB[i] = new String(byteTmp, "8859_1");
// }
// catch (Exception e) {
// CommonUtil.printDebugInfo(" CommonUtil.java.moneyToRMB() error" +
// e.toString());
// e.printStackTrace();
// }
// }
for (i = 0; i < 10; i++) {
mod = 1;
for (j = 0; j < (9 - i); j++) {
mod = mod * 10;
}
result = money / mod;
Long tmp = new Long(result);
index = tmp.intValue();
if (result == 0) {
returnStr[9 - i] = RMB[0];
}
else {
try {
returnStr[9 - i] = RMB[index];
money = money - mod * result;
}
catch (Exception e) {
CommonUtil.printDebugInfo(" CommonUtil.java.moneyToRMB() error" +
e.toString());
e.printStackTrace();
}
}
}
return returnStr;
}
/**
* user char 'c' padding befoer String str , make it length fixed
* @param String str : the string want to padding
* @param int length : the length after padding
* @param char c : which char to padding
* @return String : the string after padding
**/
public static String padding(String str, int length, char c) {
int i, len;
len = length - str.length();
for (i = 0; i < len; i++) {
str = c + str;
}
return str;
}
/**
* convert gb2312 string to 8859-1 string
* @param String str : the string want to convert
* @return String : the string after convert
*
**/
public static String gbTo8859(String str) {
// try {
// byte[] byteTmp = str.getBytes("GB2312");
// str = new String(byteTmp, "8859_1");
// }
// catch (Exception e) {
// CommonUtil.printDebugInfo(" converting gb2312 to 8859_1 error" +
// e.toString());
// e.printStackTrace();
// }
return str;
}
/**
* convert 8859-1 string to GB2312 string
* @param String str : the string want to convert
* @return String : the string after convert
*
**/
public static String toGB(String str) {
// try {
// byte[] byteTmp = str.getBytes("ISO8859_1");
// str = new String(byteTmp, "GB2312");
// }
// catch (Exception e) {
// CommonUtil.printDebugInfo(" converting 8859_1 to gb2312 error" +
// e.toString());
// e.printStackTrace();
// }
return str;
}
public static double twoPrecision(double d) {
long l = (long) (d * 100 + 0.5);
return l / 100;
}
public static float twoPrecision(float f) {
long l = (long) (f * 100 + 0.5);
return l / 100;
}
public static double iPrecision(double d, int i) {
long l = (long) (d * (10 ^ i) + 0.5);
return l / 10 ^ i;
}
/**
* Print Debug Info .If You complite program and debug over ,
* You can commit the System.out.print() sentence. then the
* compilier will skip this options.
*/
public static void printDebugInfo(java.lang.Object obj) {
// if (Prop.isDebug())
System.out.println(obj);
}
public static String native2Unicode(String s) {
if (s == null || s.length() == 0) {
return null;
}
byte[] buffer = new byte[s.length()];
for (int i = 0; i < s.length(); i++) {
buffer[i] = (byte) s.charAt(i);
}
return new String(buffer);
}
public static String unicode2Native(String s) {
if (s == null || s.length() == 0) {
return null;
}
char[] buffer = new char[s.length() * 2];
char c;
int j = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 0x100) {
c = s.charAt(i);
byte[] buf = ("" + c).getBytes();
buffer[j++] = (char) buf[0];
buffer[j++] = (char) buf[1];
}
else {
buffer[j++] = s.charAt(i);
}
}
return new String(buffer, 0, j);
}
public static String encodeToMD5(String str) {
if (str == null) {
return null;
}
String digstr = "";
MessageDigest MD = null;
try {
MD = MessageDigest.getInstance("MD5");
}
catch (Exception e) {
e.printStackTrace();
}
byte[] oldbyte = new byte[str.length()];
for (int i = 0; i < str.length(); i++) {
oldbyte[i] = (byte) str.charAt(i);
}
MD.update(oldbyte);
byte[] newbyte = null;
newbyte = MD.digest(oldbyte);
for (int i = 0; i < newbyte.length; i++) {
digstr = digstr + newbyte[i];
}
return digstr;
}
public static String foldString(String src, int length) {
if (src == null || src.equals("") || length <= 0) {
return src;
}
StringBuffer rtn = new StringBuffer();
int pos = 0;
while (pos <= src.length()) {
if (pos > 0) {
rtn.append("\n");
}
rtn.append(src.substring(pos,
pos + length <= src.length() ? pos + length :
src.length()));
pos += length;
}
return rtn.toString();
}
public static String replaceCharacter(String str) {
if (str != null) {
// String tmp = str.replace('\"','\'');
// tmp = tmp.replace('<','[');
// tmp = tmp.replace('>',']');
// tmp = tmp.replace('&','~');
String tmp = str.replaceAll("&", "@amp;");
tmp = tmp.replaceAll("\"", "@quot;");
tmp = tmp.replaceAll("<", "@lt;");
tmp = tmp.replaceAll(">", "@gt;");
// tmp = tmp.replaceAll("'", "@#146;");
// tmp = tmp.replaceAll(" ", "@nbsp;");
return tmp;
}
return "";
}
public static String undoReplaceCharacter(String str) {
if (str!=null){
// String tmp = str.replace('[','<');
// tmp = tmp.replace(']','>');
// tmp = tmp.replace('~','&');
String tmp = str.replaceAll("@amp;", "&");
tmp = tmp.replaceAll("@quot;", "\"");
tmp = tmp.replaceAll("@lt;", "<");
tmp = tmp.replaceAll("@gt;", ">");
// tmp = tmp.replaceAll("@#146;", "'");
// tmp = tmp.replaceAll("@nbsp;", " ");
// tmp = tmp.replaceAll(" ", "\n");
// tmp = tmp.replaceAll(" ", "\n");
return tmp;
}
return "";
}
static public boolean compareTime(String beforeDateTime, String afterDateTime){
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long timeCount=0;
try{
java.util.Date d1 = d.parse(beforeDateTime);
java.util.Date d2 = d.parse(afterDateTime);
return d2.after(d1);
}catch(ParseException e){
System.out.println("Date parse error!");
return false;
}
}
static public double converStrToDouble(String str)
{
str=str.trim();
if(str.indexOf(",")>0)
str=str.replace(",","");
if(str.indexOf("-")>0)
str="-"+str.replace("-","");
return Double.parseDouble(str);
}
public static void main(String[] args) {
/*
// System.out.println(strToDate("2002-03-04 5:6:7"));
String[] tst = split("111;222;333;444;555",';');
for (int i = 0; i < tst.length; i++) {
System.out.println("str->"+tst[i]);
}
System.out.println("array->"+arrayToString(tst,';'));
tst = split("111;222;333;444;555",';');
for (int i = 0; i < tst.length; i++) {
System.out.println("str->"+tst[i]);
}
*/
// String tst = "1234567890一二三四五六七八九十";
// System.out.println("foldString->"+foldString(tst,4));
// String list = "1;;2;3;4";
// String[] t = split(list, ';');
// for (int i = 0; i < t.length; i++) {
// System.out.println(t[i]);
// }
String t1="2005-3-25 16:49:30:345";
String t2 = "2005-3-25 16:49:31:362";
System.out.println(CommonUtil.compareTime(t1, t2));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -