📄 commonutil.java
字号:
/*
* @(#)CommonUtil.java
*
* Copyright (c)cyberway Inc.
* All rights reserved.
*
* This software is the confidential and proprietary information of cyberway Inc.
* ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with cyberway Inc.
*/
package cn.myapps.core.workflow.utility;
import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
/**
* All Common utilty tool function
*
* @version 1.00
* @author Wang ZhengRong
*/
public class CommonUtil {
//add char to string's back;
static final public int ADD_BACK = 1;
//add char to string's fore;
static final public int ADD_FORE = 2;
/**
* get a datetime String
*
*@return String like :2001年5月4日 星期三 14点12分14秒
*
*/
public static String getCurrentDateTime() {
String week = new String("日一二三四五六");
Date currentDate = new Date();
String str = "";
str = (2000 + currentDate.getYear() % 100) + "年 ";
str = str + currentDate.getMonth() + "月";
str = str + currentDate.getDate() + "日 ";
int day = currentDate.getDay();
str = str + "星期" + week.substring(day, day + 1) + " ";
str = str + currentDate.getHours() + "点 ";
str = str + currentDate.getMinutes() + "分 ";
str = str + currentDate.getSeconds() + "秒 ";
return str;
} //String getCurrentDateTime() end;
/**
* get next id
* For Example : getNext("009",3) will return "010";
*
*@param curr current id
*@param len total length
*@return public static String
*/
public static String getNext(String curr, int len) {
if (curr == null) {
return CommonUtil.setChar('0', len);
}
String ret = "";
long lValue = new Long(curr).longValue() + 1;
ret += lValue;
ret = CommonUtil.setChar('0', len - ret.length()) + ret;
return ret;
}
public static boolean getBoolean(String str) {
if (isBooleanString(str)) {
return (new Boolean(str)).booleanValue();
}
else {
return false;
}
}
/**
* get special length string with the same char
* For Example : setChar('0',4) will return "0000"
*
*@param chr char
*@param totalLen total length
*
*@return String
*/
public static String setChar(char chr, int totalLen) {
return CommonUtil.setChar("", chr, totalLen, CommonUtil.ADD_FORE);
}
/**
* get special length string with the same char
* For Example : setChar("12",'0',4) will return "0012"
*
*@str String need to filled with
*@param chr char
*@param totalLen total length
*
*@return String
*/
public static String setChar(String str, char chr, int totalLen) {
return CommonUtil.setChar(str, chr, totalLen, CommonUtil.ADD_FORE);
}
/**
* get special length string with the same char
* For Example : setChar("12",'0',4,CommonUtil.ADD_BACK) will return "1200"
*
*@str String need to filled with
*@param chr char
*@param totalLen total length
*@param position wh
*@author Administrator
*@return String
*/
public static String setChar(String str, char chr, int totalLen, int position) {
if (totalLen < 0 || str == null) {
return str;
}
int sLen = str.length();
String ret = str;
for (int i = 0; i < totalLen - sLen; i++) {
if (position == CommonUtil.ADD_BACK) {
ret = ret + chr;
}
else {
ret = chr + ret;
}
} //end of for
return ret;
}
/**
* Validate a date string format YYYY-MM-DD or YYYY-MM-DD HH:MM:SS
* @param strDate a string representing date
* @return boolean true if format is "YYYY-MM-DD HH:MM:SS" else return false
*/
static public boolean isValidDate(String strDate) {
try {
return strToDate(strDate) != null;
}
catch (Exception e) {
return false;
}
}
/**
* check if the string are composite of number character
* for example "-48594.94395" return true "454kdf.94" return false
*
* @param str String
*
* @return true or false
*/
static public boolean isNumberString(String str) {
if (str == null || str.trim().equals("")) {
return false;
}
try {
double t = Double.valueOf(str).doubleValue();
}
catch (Exception e) {
CommonUtil.printDebugInfo("it is not a number!");
return false;
}
return true;
}
static public boolean isBooleanString(String str) {
if (str == null || str.trim().equals("")) {
return false;
}
try {
Boolean t = new Boolean(str);
}
catch (Exception e) {
CommonUtil.printDebugInfo("it is not a boolean!");
return false;
}
return true;
}
static public int getFirstIndexInStrArray(String[] list, String str) {
int pos = -1;
if (list != null && list.length > 0) {
for (int i = 0; i < list.length; i++) {
if (list[i] != null && list[i].equals(str)) {
pos = i;
break;
}
}
}
return pos;
}
/**
* check if the string are composite of letter
* for example "iefKASDFK" return true "454kdfadf4" return false
*
* @param str String
*
* @return true or false
*/
static public boolean isLetterString(String str) {
if (str == null) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (!Character.isLetter(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* check if the string are composite of letter or digit
* for example "iefKASDFK" return true "454kdfadf4" return true "^rek/." return false
*
* @param str String
*
* @return true or false
*/
static public boolean isLetterOrDigitString(String str) {
if (str == null) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (!Character.isLetterOrDigit(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* check if the string are composite of number character and length less than special bit number
* for example "-48594.94395" return true "454kdf.94" return false
*
* @param str String
*
* @return true or false
*/
static public boolean isNumberString(String str, int bit) {
if (bit <= 0) {
return false;
}
if (isNumberString(str) && str.length() <= bit) {
return true;
}
else {
return false;
}
}
/**
* getDateStr get a string with format YYYY-MM-DD from a Date object
* @param Date date
* @return String
*/
static public String getDateStr(Date date) {
if (date == null) {
return "";
}
int year = date.getYear() + 1900;
int month = date.getMonth() + 1;
int day = date.getDate();
return year + "-" + month + "-" + day;
}
/**
* getDateStr get a string with format YYYY-MM-DD from a Date object
* @param Date date
* @return String
*/
static public String getCurrentDateToSerializeNo() {
Date date = new Date();
int year = date.getYear() + 1900;
int month = date.getMonth() + 1;
int day = date.getDate();
return year + "-" + month + "-" + day;
}
/**
* getTimeStr get a string with format HH24:MI:SS from a Time object
* @param java.sql.Time time
* @return String
*/
static public String getTimeStr(java.sql.Time time) {
if (time == null) {
return "";
}
return " " + time.getHours()
+ ":" + time.getMinutes()
+ ":" + "00";
}
/**
* convert a string with format YYYY-MM-DD to a java.sql.Date object
* For Example : Date String "2000-12-10"
*
* @param dateString format "YYYY-MM-DD" or "YYYY-MM-DD HH:MM:SS"
*
* @return java.sql.Date type object
*/
static public java.sql.Date strToDate(String str) {
String dtstr = new String(str);
dtstr.replace('/', '-');
while (dtstr.indexOf(" ") >= 0) {
dtstr = dtstr.substring(0, dtstr.indexOf(" ")) +
dtstr.substring(dtstr.indexOf(" ") + 1);
}
if (dtstr == null
|| dtstr.length() < 8
) {
return null;
}
int len = str.length();
String year = "0";
while (!dtstr.equals("")
&& !dtstr.startsWith("-")
&& !dtstr.startsWith(":")
&& !dtstr.startsWith(" ")) {
year += dtstr.charAt(0);
dtstr = dtstr.substring(1);
}
String month = "0";
dtstr = dtstr.trim().length() > 0 ? dtstr.trim().substring(1) : "";
while (!dtstr.equals("")
&& !dtstr.startsWith("-")
&& !dtstr.startsWith(":")
&& !dtstr.startsWith(" ")) {
month += dtstr.charAt(0);
dtstr = dtstr.trim().substring(1);
}
String date = "0";
dtstr = dtstr.trim().length() > 0 ? dtstr.trim().substring(1) : "";
while (!dtstr.equals("")
&& !dtstr.startsWith("-")
&& !dtstr.startsWith(":")
&& !dtstr.startsWith(" ")) {
date += dtstr.charAt(0);
dtstr = dtstr.trim().substring(1);
}
String hour = "0";
dtstr = dtstr.trim();
while (!dtstr.equals("")
&& !dtstr.startsWith("-")
&& !dtstr.startsWith(":")
&& !dtstr.startsWith(" ")) {
hour += dtstr.charAt(0);
dtstr = dtstr.trim().substring(1);
}
String minute = "0";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -