📄 util.java
字号:
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 getHTML(String str) {
String strret = null;
URL rTmp = null;
InputStream ins = null;
BufferedReader breader = null;
InputStreamReader isreader = null;
try {
rTmp = new URL(str);
ins = rTmp.openStream();
isreader = new InputStreamReader(ins);
breader = new BufferedReader(isreader);
String info = breader.readLine();
strret = info;
info = breader.readLine();
while (info != null) {
strret = strret + "\n" + info;
info = breader.readLine();
}
}
catch (Exception e) {
//e.printStackTrace(System.err);
//return null;
}
finally {
try {
if (breader != null) {
breader.close();
}
}
catch (IOException ex) {
}
try {
if (isreader != null) {
isreader.close();
}
}
catch (IOException ex1) {
}
try {
if (ins != null) {
ins.close();
}
}
catch (IOException ex2) {
}
return strret;
}
}
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;
}
/**
* 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;
}
public static String[] getPagesign(Locale locale) {
String[] pagessign = {
Messages.getMessage(locale, "pages.first"),
Messages.getMessage(locale, "pages.previous"),
Messages.getMessage(locale, "pages.next"),
Messages.getMessage(locale, "pages.end")};
return pagessign;
}
public static void main(String[] args) {
String t = "abcdfg\"sfdfaeeed<a href=>ghjfghj";
t = escapeForSpecial(t);
System.out.println(t);
System.out.println(unescapeFromXML(t));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -