📄 util.java
字号:
}
private static final char[] zeroArray = "0000000000000000".toCharArray();
/**
* Pads the supplied String with 0's to the specified length and returns the
* result as a new String. For example, if the initial String is "9999" and
* the desired length is 8, the result would be "00009999". This type of
* padding is useful for creating numerical values that need to be stored
* and sorted as character data. Note: the current implementation of this
* method allows for a maximum <tt>length</tt> of 16.
*
* @param string
* the original String to pad.
* @param length
* the desired length of the new padded String.
* @return a new String padded with the required number of 0's.
*/
public static final String zeroPadString(String string, int length) {
if (string == null || string.length() > length) {
return string;
}
StringBuffer buf = new StringBuffer(length);
buf.append(zeroArray, 0, length - string.length()).append(string);
return buf.toString();
}
/**
* Formats a Date as a fifteen character long String made up of the Date's
* padded millisecond value.
*
* @return a Date encoded as a String.
*/
public static final String dateToMillis(Date date) {
return zeroPadString(Long.toString(date.getTime()), 15);
}
/**
* Formats a Date as a fifteen character long String made up of the Date's
* padded millisecond value.
*
* @return a Date encoded as a String.
*/
@SuppressWarnings("unchecked")
public static final String collectionToString(Collection c, String spilt) {
if (c == null) {
return null;
}
if (spilt == null) {
return null;
}
String ret = "";
ArrayList a = new ArrayList(c);
try {
for (int i = 0; i < a.size(); i++) {
String t = (String) a.get(i);
if (i == a.size() - 1) {
ret = ret + t;
} else {
ret = ret + t + spilt;
}
}
return ret;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String genPassword(int length) {
if (length < 1) {
return null;
}
String[] strChars = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i",
"j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a" };
// 没有0,o,l和I,以免误解
StringBuffer strPassword = new StringBuffer();
int nRand = (int) java.lang.Math.round(java.lang.Math.random() * 100);
for (int i = 0; i < length; i++) {
nRand = (int) java.lang.Math.round(java.lang.Math.random() * 100);
strPassword.append(strChars[nRand % (strChars.length - 1)]);
// strPassword += strChars[nRand % (strChars.length - 1)];
}
return strPassword.toString();
}
public static String genNumPassword(int length) {
if (length < 1) {
return null;
}
String[] strChars = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
StringBuffer strPassword = new StringBuffer();
int nRand = (int) java.lang.Math.round(java.lang.Math.random() * 100);
for (int i = 0; i < length; i++) {
nRand = (int) java.lang.Math.round(java.lang.Math.random() * 100);
strPassword.append(strChars[nRand % (strChars.length - 1)]);
// strPassword += strChars[nRand % (strChars.length - 1)];
}
return strPassword.toString();
}
public static String genEmptyString(int length) {
if (length < 1) {
return null;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
sb.append(" ");
}
return sb.toString();
}
public static String getAsciiString(int digit) {
byte ret[] = new byte[1];
ret[0] = (byte) digit;
return new String(ret);
}
public static int getAsciiNum(String s) {
if (s.length() < 1) {
return 0;
}
byte b = s.getBytes()[0];
return b;
}
public static String getCurrTime() {
Date now = new Date();
SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String s = outFormat.format(now);
return s;
}
public static String formatDate(Date date, String format) {
SimpleDateFormat outFormat = new SimpleDateFormat(format);
return outFormat.format(date);
}
/**
* Formats a Date object to return a date using the global locale.
*/
public static String formatDate(Date date) {
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
return outFormat.format(date);
}
/**
* Formats a Date object to return a date and time using the global locale.
*/
public static String formatDateTime(Date date) {
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return outFormat.format(date);
}
public static String formatDate2(Date myDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String strDate = formatter.format(myDate);
return strDate;
}
public static String formatDate3(Date myDate) {
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
String strDate = formatter.format(myDate);
return strDate;
}
public static String formatDate4(Date myDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String strDate = formatter.format(myDate);
return strDate;
}
public static String formatDate5(Date myDate) {
String strDate = getYear(myDate) + "-" + getMonth(myDate) + "-" + getDay(myDate);
return strDate;
}
public static String formatDate6(Date myDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String strDate = formatter.format(myDate);
return strDate;
}
public static long Date2Long(int year, int month, int date) {
Calendar cld = Calendar.getInstance();
month = month - 1;
cld.set(year, month, date);
return cld.getTime().getTime();
}
public static long Time2Long(int year, int month, int date, int hour, int minute, int second) {
Calendar cld = Calendar.getInstance();
month = month - 1;
cld.set(year, month, date, hour, minute, second);
return cld.getTime().getTime();
}
public static int getYear(long t) {
Calendar cld = Calendar.getInstance();
if (t > 0) {
cld.setTime(new java.util.Date(t));
}
return cld.get(Calendar.YEAR);
}
public static int getMonth(long t) {
Calendar cld = Calendar.getInstance();
if (t > 0) {
cld.setTime(new java.util.Date(t));
}
return cld.get(Calendar.MONTH) + 1;
}
public static int getDay(long t) {
Calendar cld = Calendar.getInstance();
if (t > 0) {
cld.setTime(new java.util.Date(t));
}
return cld.get(Calendar.DAY_OF_MONTH);
}
public static int getHour(long t) {
Calendar cld = Calendar.getInstance();
if (t > 0) {
cld.setTime(new java.util.Date(t));
}
return cld.get(Calendar.HOUR_OF_DAY);
}
public static int getMinute(long t) {
Calendar cld = Calendar.getInstance();
if (t > 0) {
cld.setTime(new java.util.Date(t));
}
return cld.get(Calendar.MINUTE);
}
public static int getSecond(long t) {
Calendar cld = Calendar.getInstance();
if (t > 0) {
cld.setTime(new java.util.Date(t));
}
return cld.get(Calendar.SECOND);
}
public static int getYear(Date date) {
Calendar cld = Calendar.getInstance();
cld.setTime(date);
return cld.get(Calendar.YEAR);
}
public static int getMonth(Date date) {
Calendar cld = Calendar.getInstance();
cld.setTime(date);
return cld.get(Calendar.MONTH) + 1;
}
public static int getDay(Date date) {
Calendar cld = Calendar.getInstance();
cld.setTime(date);
return cld.get(Calendar.DAY_OF_MONTH);
}
public static int getHour(Date date) {
Calendar cld = Calendar.getInstance();
cld.setTime(date);
return cld.get(Calendar.HOUR_OF_DAY);
}
public static int getMinute(Date date) {
Calendar cld = Calendar.getInstance();
cld.setTime(date);
return cld.get(Calendar.MINUTE);
}
public static int getSecond(Date date) {
Calendar cld = Calendar.getInstance();
cld.setTime(date);
return cld.get(Calendar.SECOND);
}
public static int getYear() {
Calendar cld = Calendar.getInstance();
cld.setTime(new java.util.Date());
return cld.get(Calendar.YEAR);
}
public static int getMonth() {
Calendar cld = Calendar.getInstance();
cld.setTime(new java.util.Date());
return cld.get(Calendar.MONTH) + 1;
}
public static int getDay() {
Calendar cld = Calendar.getInstance();
cld.setTime(new java.util.Date());
return cld.get(Calendar.DAY_OF_MONTH);
}
public static String replaceComma(String text) {
if (text != null) {
text = text.replaceAll(",", ",");
}
return text;
}
public static String replaceBr(String text) {
if (text != null) {
text = text.replaceAll("\n", "<BR>");
}
return text;
}
public static long getLongTime() {
return System.currentTimeMillis();
}
/**
* Check a string null or blank.
*
* @param param
* string to check
* @return boolean
*/
public static boolean nullOrBlank(String param) {
return (param == null || param.length() == 0 || param.trim().equals("")) ? true : false;
}
public static String notNull(String param) {
return param == null ? "" : param.trim();
}
/**
* Parse a string to boolean.
*
* @param param
* string to parse
* @return boolean value, if param begin with(1,y,Y,t,T) return true, on
* exception return false.
*/
public static boolean parseBoolean(String param) {
if (nullOrBlank(param)) {
return false;
}
switch (param.charAt(0)) {
case '1':
case 'y':
case 'Y':
case 't':
case 'T':
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -