📄 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 + -