📄 genericutil.java
字号:
package com.ntsky.filter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
/**
* GenericUtil
*
*@author liwei
*@version 1.0
*/
public class GenericUtil {
/**
* Gets the NullOrBlank attribute of the GenericUtil object
*
*@param src Description of Parameter
*@return The NullOrBlank value
*@since empty
*/
public static boolean isNullOrBlank(String src) {
return src == null || src.trim().length() == 0;
}
/**
* Gets the String attribute of the GenericUtil class
*
*@param ss Description of Parameter
*@param prefix Description of Parameter
*@param postfix Description of Parameter
*@param seperator Description of Parameter
*@return The String value
*@since empty
*/
public static String getString(String[] ss, String prefix, String postfix, String seperator) {
StringBuffer sb = new StringBuffer();
if(ss != null) {
for(int i = 0; i < ss.length; i++) {
sb.append(prefix).append(ss[i]).append(postfix).append(seperator);
}
}
if(sb.length() > 0) {
sb.delete(sb.length() - seperator.length(), sb.length());
}
return sb.toString();
}
/**
* Gets the Time attribute of the GenericUtil class
*
*@param date Description of Parameter
*@param pattern Description of Parameter
*@return The Time value
*@since empty
*/
public static String getTime(Date date, String pattern) {
DateFormat df = new SimpleDateFormat(pattern);
String time = df.format(date);
return time;
}
/**
* Gets the Time attribute of the GenericUtil class
*
*@param date Description of Parameter
*@param oldPattern Description of Parameter
*@param pattern Description of Parameter
*@return The Time value
*@since empty
*/
public static String getTime(String date, String oldPattern, String pattern) {
DateFormat df = new SimpleDateFormat(oldPattern);
Date time = null;
try {
time = df.parse(date);
}
catch(ParseException ex) {
}
if(time == null) {
return "";
}
return getTime(time, pattern);
}
/**
* Gets the NextNDays attribute of the GenericUtil class
*
*@param date Description of Parameter
*@param pattern Description of Parameter
*@param n Description of Parameter
*@return The NextNDays value
*@since empty
*/
public static String getNextNDays(Date date, String pattern, int n) {
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(Calendar.DATE, n);
DateFormat df = new SimpleDateFormat(pattern);
String time = df.format(cl.getTime());
return time;
}
/**
* Gets the NextNDays attribute of the GenericUtil class
*
*@param begin Description of Parameter
*@param pattern Description of Parameter
*@param n Description of Parameter
*@return The NextNDays value
*@since empty
*/
public static String getNextNDays(String begin, String pattern, int n) {
DateFormat df = new SimpleDateFormat(pattern);
Date date = null;
try {
date = df.parse(begin);
}
catch(ParseException ex) {
}
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(Calendar.DATE, n);
String time = df.format(cl.getTime());
return time;
}
/**
* Gets the NextNMonths attribute of the GenericUtil class
*
*@param begin Description of Parameter
*@param pattern Description of Parameter
*@param n Description of Parameter
*@return The NextNMonths value
*@since empty
*/
public static String getNextNMonths(String begin, String pattern, int n) {
DateFormat df = new SimpleDateFormat(pattern);
Date date = null;
try {
date = df.parse(begin);
}
catch(ParseException ex) {
}
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(Calendar.MONTH, n);
String time = df.format(cl.getTime());
return time;
}
/**
* Gets the NextNHours attribute of the GenericUtil class
*
*@param begin Description of Parameter
*@param pattern Description of Parameter
*@param n Description of Parameter
*@return The NextNHours value
*@since empty
*/
public static String getNextNHours(String begin, String pattern, int n) {
DateFormat df = new SimpleDateFormat(pattern);
Date date = null;
try {
date = df.parse(begin);
}
catch(ParseException ex) {
}
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(Calendar.HOUR, n);
String time = df.format(cl.getTime());
return time;
}
/**
* Gets the Number attribute of the GenericUtil class
*
*@param src Description of Parameter
*@return The Number value
*@since empty
*/
public static boolean isNumber(String src) {
try {
Long.parseLong(trim(src));
}
catch(NumberFormatException ex) {
return false;
}
return true;
}
/**
* Gets the MobilePhoneNo attribute of the GenericUtil class
*
*@param src Description of Parameter
*@return The MobilePhoneNo value
*@since empty
*/
public static boolean isMobilePhoneNo(String src) {
src = trim(src);
return isNumber(src) && src.startsWith("13") && src.length() == 11;
}
/**
* Gets the CTPhoneNo attribute of the GenericUtil class
*
*@param src Description of Parameter
*@return The CTPhoneNo value
*@since empty
*/
public static boolean isCTPhoneNo(String src) {
src = trim(src);
return isNumber(src) && src.startsWith("0") && (src.length() == 11 || src.length() == 12);
}
/**
* Description of the Method
*
*@param phoneNo Description of Parameter
*@param mask Description of Parameter
*@param from Description of Parameter
*@param to Description of Parameter
*@return Description of the Returned Value
*@since empty
*/
public static String maskPhoneNo(String phoneNo, String mask, int from, int to) {
String pre = phoneNo.substring(0, from);
String end = phoneNo.substring(to);
return pre + mask + end;
}
/**
* Description of the Method
*
*@param begin Description of Parameter
*@param end Description of Parameter
*@param pattern Description of Parameter
*@return Description of the Returned Value
*@since empty
*/
public static boolean before(String begin, String end, String pattern) {
DateFormat df = new SimpleDateFormat(pattern);
try {
Date beginDate = df.parse(begin);
Date endDate = df.parse(end);
return beginDate.before(endDate);
}
catch(Exception ex) {
return false;
}
}
/**
* Description of the Method
*
*@param ss Description of Parameter
*@param s Description of Parameter
*@return Description of the Returned Value
*@since empty
*/
public static boolean IsAll(String[] ss, String s) {
if(ss == null || ss.length == 0 || s == null) {
return false;
}
for(int i = 0; i < ss.length; i++) {
if(!s.equals(ss[i])) {
return false;
}
}
return true;
}
/**
* Description of the Method
*
*@param ss Description of Parameter
*@param s Description of Parameter
*@return Description of the Returned Value
*@since empty
*/
public static boolean contains(String[] ss, String s) {
if(ss == null || ss.length == 0 || s == null) {
return false;
}
for(int i = 0; i < ss.length; i++) {
if(s.equals(ss[i])) {
return true;
}
}
return false;
}
/**
* Description of the Method
*
*@param src Description of Parameter
*@return Description of the Returned Value
*@since empty
*/
public static String trim(String src) {
if(src == null) {
return "";
}
return src.trim();
}
/**
* <p>
*
* Splits the provided text into an array, separator specified. This is an
* alternative to using StringTokenizer.</p> <p>
*
* The separator is not included in the returned String array. Adjacent
* separators are treated as one separator.</p> <p>
*
* A <code>null</code> input String returns <code>null</code>.</p> <pre>
* StringUtils.split(null, *) = null
* StringUtils.split("", *) = []
* StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
* StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
* StringUtils.split("a:b:c", '.') = ["a:b:c"]
* StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
* StringUtils.split("a b c", ' ') = ["a", "b", "c"]
* </pre>
*
*@param str the String to parse, may be null
*@param separatorChar the character used as the delimiter, <code>null</code>
* splits on whitespace
*@return an array of parsed Strings, <code>null</code> if
* null String input
*@since empty
*/
public static String[] split(String str, char separatorChar) {
// Performance tuned for 2.0 (JDK1.4)
if(str == null) {
return null;
}
int len = str.length();
if(len == 0) {
return new String[0];
}
List list = new ArrayList();
int i = 0;
int start = 0;
boolean match = false;
while(i < len) {
if(str.charAt(i) == separatorChar) {
if(match) {
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
match = true;
i++;
}
if(match) {
list.add(str.substring(start, i));
}
return (String[])list.toArray(new String[list.size()]);
}
/**
* Description of the Method
*
*@param url Description of Parameter
*@return Description of the Returned Value
*@since empty
*/
public static String urlEncode(String url) {
return urlEncode(url, "iso8859-1");
}
/**
* Description of the Method
*
*@param url Description of Parameter
*@param encode Description of Parameter
*@return Description of the Returned Value
*@since empty
*/
public static String urlEncode(String url, String encode) {
url = trim(url);
try {
return URLEncoder.encode(url, encode);
}
catch(UnsupportedEncodingException ex) {
ex.printStackTrace();
return url;
}
}
/**
* Description of the Method
*
*@param req Description of Parameter
*@return Description of the Returned Value
*@since empty
*/
public static String generateUrl(HttpServletRequest req) {
StringBuffer url = new StringBuffer();
String scheme = req.getScheme();
int port = req.getServerPort();
url.append(scheme).append("://");
url.append(req.getServerName());
if((scheme.equalsIgnoreCase("http") && port != 80) || (scheme.equalsIgnoreCase("https") && port != 443)) {
url.append(":").append(port);
}
url.append(req.getContextPath());
return url.toString();
}
/**
* Description of the Method
*
*@return Description of the Returned Value
*@since empty
*/
public static synchronized String generateTokenId() {
return String.valueOf(new Date().getTime());
}
/**
* The main program for the GenericUtil class
*
*@param ags The command line arguments
*@exception Exception Description of Exception
*@since empty
*/
public static void main(String[] ags) throws Exception {
System.out.println(GenericUtil.getTime(new Date(), "yyyy-MM-01"));
System.out.println(GenericUtil.getNextNDays(new Date(), "yyyy-MM-dd", 1));
System.out.println(GenericUtil.getNextNDays("2004-01-02", "yyyy-MM-dd", 1));
System.out.println(GenericUtil.before("2004-01-02", "2004-01-05", "yyyy-MM-dd"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -