?? baseutil.java
字號:
package com.comm.util;
import java.sql.Date;
import java.text.*;
import java.util.*;
import java.util.Enumeration;
public class BaseUtil {
public BaseUtil() {
}
/**
* 如果s = null return ""\uFFFD? 否則返回s 本身\uFFFD?
*
* @param s String
* @return String
*/
public static String toString(String s) {
return (s == null) ? "" : s;
}
/**
* 如果s = null 返回 "";
* 如果s的長度大于length,則返回 前length-3長度的字串并加上\uFFFD?...\uFFFD?;
* 否則返回 s 本身\uFFFD?
* @param s String
* @param length int
* @return String
*/
public static String trimString(String s, int length) {
if (s == null) {
return "";
} else if (s.length() > length) {
return s.substring(1, length - 3) + "...";
} else {
return s;
}
}
public static String toString(Date date, String formate) {
DateFormat df = new SimpleDateFormat(formate);
return (null == date) ? "" : df.format(date);
}
/**
* 如果date \uFFFD? null ,返回\uFFFD?\uFFFD\uFFFD?\uFFFD,否則返回date.toString();
* @param date Date
* @return String
*/
public static String toString(Date date) {
return (null == date) ? "" : date.toString();
}
/**
* 格式化為“yyyy-MM-dd”的字符\uFFFD?
* @param date Date
* @return String
*/
public static String toShortDate(java.util.Date date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return (null == date) ? "" : df.format(date);
}
/**
* 格式化為“yyyy-MM-dd HH:mm:ss”的字符\uFFFD?
*
* @param date Date
* @return String
*/
public static String toLongDate(java.util.Date date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return (null == date) ? "" : df.format(date);
}
/**
* 格式化為“yyyy-MM-dd HH:mm”的字符\uFFFD?
*
* @param date Date
* @return String
*/
public static String toDateMin(java.util.Date date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return (null == date) ? "" : df.format(date);
}
/**
* 格式化為“HH:mm”的字符\uFFFD?
* @param date Date
* @return String
*/
public static String toHourMin(java.util.Date date) {
DateFormat df = new SimpleDateFormat("HH:mm");
return (null == date) ? "" : df.format(date);
}
/**
* 返回整數的字符串形式\uFFFD? \uFFFD? 1 \uFFFD?>\uFFFD?1”;
* 如果 i \uFFFD? \uFFFD?1 ,則轉換為\uFFFD?\uFFFD\uFFFD?\uFFFD\uFFFD??
* @param i int
* @return String
*/
public static String toString(int i) {
return (i == -1) ? "" : String.valueOf(i);
}
/**
* 返回long類型的字符串形式;如1L轉換為\uFFFD??1”;
*
* @param l long
* @return String
*/
public static String toString(long l) {
return String.valueOf(l);
}
/**
* 返回double類型的字符串形式;如1.32轉換為\uFFFD??1.32”;
*
* @param d double
* @return String
*/
public static String toString(double d) {
return String.valueOf(d);
}
/**
* 返回Object類型的字符串形式;
* 如果為null 則返回\uFFFD?\uFFFD\uFFFD?\uFFFD;否則返回object.toString();
* @param object Object
*/
public static String toString(Object object) {
return (null == object) ? "" : object.toString();
}
/**
*
* 如果object = null 返回 "";
* <br>如果object.toString()的長度大于length\uFFFD?
* 則返\uFFFD? 前length-4長度的字串并加上\uFFFD?....\uFFFD?;<br>
* 否則返回 s 本身\uFFFD?
* @param object Object
* @param length int
* @return String
*/
public static String toString(Object object, int length) {
if (object == null) {
return "";
} else if (object.toString().length() > length) {
return object.toString().substring(0, length - 4) + "....";
} else {
return object.toString();
}
}
/**
* 按字符串內容構\uFFFD?\uFFFD日期:
* <br>格式\uFFFD? “yyyy-mm-dd hh:mm:ss.fffffffff”,“yyyy-mm-dd hh:mm:ss\uFFFD?
* 或\uFFFD?\uFFFDyyyy-mm-dd”有效\uFFFD??<br>
* <br>如果 s =null 或\uFFFD?\uFFFD\uFFFD?\uFFFD,則返回null\uFFFD?<br>
*
* @param s String
* @return Date
*/
public static Date toDate(String s) {
if (null == s || "".equals(s)) {
return null;
} else {
if (s.indexOf(":") < 0) {
return Date.valueOf(s);
} else if (s.indexOf(":") != s.lastIndexOf(":")) {
return new Date(java.sql.Timestamp.valueOf(s).getTime());
} else {
return new Date(java.sql.Timestamp.valueOf(s.concat(":0")).
getTime());
}
}
}
/**
* 如果 s =null ,則返回null\uFFFD?
* 否則按s.toString()字符串內容構造日期:
* <br>格式\uFFFD? “yyyy-mm-dd hh:mm:ss.fffffffff”,“yyyy-mm-dd hh:mm:ss\uFFFD?
* 或\uFFFD?\uFFFDyyyy-mm-dd”有效\uFFFD??
*
* @param s String
* @return Date
*/
public static Date toDate(Object s) {
return toDate(toString(s));
}
/**
* 返回d
* @param d Date
* @return Date
*/
public static Date toDate(Date d) {
return d;
}
/**
* 如果s \uFFFD? null 返回 null
* 否則按s.toString()字符串內容構造日\uFFFD?,并設置日期的時分秒為\uFFFD?23\uFFFD?59\uFFFD?59”:
* <br>格式\uFFFD? “yyyy-mm-dd hh:mm:ss.fffffffff”,“yyyy-mm-dd hh:mm:ss\uFFFD?
* 或\uFFFD?\uFFFDyyyy-mm-dd”有效\uFFFD??<br>
*
* @param s Object
* @return Date
*/
public static Date toEndDate(Object s) {
return getDayEnd(toDate(toString(s)));
}
/**
* 如果s \uFFFD? null 返回 null
* 否則設置日期的時分秒為\uFFFD??23\uFFFD?59\uFFFD?59”:
* @param s Object
* @return Date
*/
public static Date getDayEnd(Date d) {
if (d == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(d);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return new Date(c.getTimeInMillis());
}
/**
* 如果s \uFFFD? null 返回 null
* 否則設置日期的時分秒為\uFFFD??0\uFFFD?0\uFFFD?0”:
* @param d Date
* @return Date
*/
public static Date getDayStart(Date d) {
if (d == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(d);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return new Date(c.getTimeInMillis());
}
/**
* 如果s \uFFFD? null或\uFFFD?\uFFFD\uFFFD?\uFFFD,返回 \uFFFD?1,否則返回字符串對應的整數;如\uFFFD??10”-\uFFFD?> 10
* @param s String
* @return int
*/
public static int toInt(String s) {
return (null == s || "".equals(s)) ? -1 : Integer.valueOf(s).intValue();
}
public static long toLong(String s) {
return (null == s || "".equals(s)) ? -1 : Long.valueOf(s).longValue();
}
/**
* 如果s \uFFFD? null或\uFFFD?\uFFFD\uFFFD?\uFFFD,返回 null,否則返回字符串對應的Double\uFFFD?
* <br>ex. \uFFFD?10.0”-\uFFFD?> new Double(10.0)<br>
* @param s String
* @return Double
*/
public static Double toDouble(String s) {
return (null == s || "".equals(s)) ? null : Double.valueOf(s);
}
/**
* 如果s \uFFFD? null或\uFFFD?\uFFFD\uFFFD?\uFFFD,返回 \uFFFD?1.0,否則返回字符串對應的double數;
* <br>如\uFFFD??10.1”-\uFFFD?> 10.1
* @param s String
* @return int
*/
public static double todouble(String s) {
return (null == s || "".equals(s)) ? -1 : Double.valueOf(s).doubleValue();
}
/**
* 如果s \uFFFD? null 返回 \uFFFD?1;否則返回s.toSting()對應的int
* @param s Object
* @return int
*/
public static int toInt(Object s) {
return toInt(toString(s));
}
/**
* 如果s \uFFFD? null或\uFFFD?\uFFFD\uFFFD?? 返回 null;否則返\uFFFD? s 對應的Integer
*<br> \uFFFD? \uFFFD?3”-\uFFFD?> new Integer(3)
* @param string String
* @return Object
*/
public static Integer toInteger(String s) {
if (s == null || "".equals(s) || s.equals("-1")
|| "null".equalsIgnoreCase(s)) {
return null;
} else {
return Integer.valueOf(s);
}
}
/**
* 如果s \uFFFD? null 返回 null;否則返\uFFFD? s.toString() 對應的Integer
* <br>\uFFFD? \uFFFD?3”-\uFFFD?> new Integer(3)<br>
* @param string String
* @return Object
*/
public static Integer toInteger(Object o) {
if (o == null) {
return null;
} else {
return Integer.valueOf(o.toString());
}
}
/**
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -