diff --git a/src/com/jsh/util/AssetConstants.java b/src/com/jsh/util/AssetConstants.java new file mode 100644 index 000000000..1940450e0 --- /dev/null +++ b/src/com/jsh/util/AssetConstants.java @@ -0,0 +1,119 @@ +package com.jsh.util; + +/** + * 定义资产管理常量 + * @author jishenghua + */ +public interface AssetConstants +{ + /** + * 公共常量 + * @author jishenghua + */ + public class Common + { + + } + + /** + * 资产常量--导入导出excel表格业务相关 + * @author jishenghua + */ + public class BusinessForExcel + { + /** + * 资产名称常量 + */ + public static final int EXCEL_ASSETNAME = 0; + + /** + * 资产类型常量 + */ + public static final int EXCEL_CATEGORY = 1; + + /** + * 资产单价 + */ + public static final int EXCEL_PRICE = 2; + + /** + * 用户 + */ + public static final int EXCEL_USER = 3; + + /** + * 购买日期 + */ + public static final int EXCEL_PURCHASE_DATE = 4; + + /** + * 资产状态 + */ + public static final int EXCEL_STATUS = 5; + + /** + * 位置 + */ + public static final int EXCEL_LOCATION = 6; + + /** + * 资产编号 + */ + public static final int EXCEL_NUM = 7; + + /** + * 序列号 + */ + public static final int EXCEL_SERIALNO = 8; + + /** + * 有效日期 + */ + public static final int EXCEL_EXPIRATION_DATE = 9; + + /** + * 保修日期 + */ + public static final int EXCEL_WARRANTY_DATE = 10; + + /** + * 供应商 + */ + public static final int EXCEL_SUPPLIER = 11; + + /** + * 标签 + */ + public static final int EXCEL_LABLE = 12; + + /** + * 描述 + */ + public static final int EXCEL_DESC = 13; + + /** + * 表头 + */ + public static final int EXCEL_TABLE_HEAD = 0; + + /** + * 状态 --在库 + */ + public static final int EXCEl_STATUS_ZAIKU = 0; + + /** + * 状态 --在用 + */ + public static final int EXCEl_STATUS_INUSE = 1; + + /** + * 状态 -- 消费 + */ + public static final int EXCEl_STATUS_CONSUME = 2; + + /** + * action返回excel结果 + */ + public static final String EXCEL = "excel"; + } +} diff --git a/src/com/jsh/util/BeanFactoryUtil.java b/src/com/jsh/util/BeanFactoryUtil.java new file mode 100644 index 000000000..fe01bf228 --- /dev/null +++ b/src/com/jsh/util/BeanFactoryUtil.java @@ -0,0 +1,127 @@ +package com.jsh.util; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.context.support.FileSystemXmlApplicationContext; + +/** + * 获取spring配置中的bean对象,是单例,只会加载一次,请注意使用 + * 注意:此工具类默认处理UI组件WEB-INF目录下的applicationContext.xml配置文件,请注意文件 名和路径 + * @author jishenghua + * @qq 7 5 2 7 1 8 9 2 0 + * @version V1.0 + */ +public class BeanFactoryUtil +{ + private static BeanFactoryUtil defaultBeanFactory; + + private ApplicationContext defaultAC = null; + + //private ApplicationContext autoLoadAC = null; + + private static BeanFactoryUtil specialBeanFactory; + + private ApplicationContext specialAC = null; + + private static Map beanMap = new HashMap(); + + //private Logger log = Logger.getLogger(BeanFactoryUtil.class); + + /** + * 私有构造函数,默认为UI组件WEB-INF目录下的applicationContext.xml配置文件 + */ + private BeanFactoryUtil() + { + String fileUrl = PathTool.getWebinfPath(); + //这里只对UI组件WEB-INF目录下的applicationContext.xml配置文件 + defaultAC = new FileSystemXmlApplicationContext( new + String[]{fileUrl + + "spring/basic-applicationContext.xml", + fileUrl + "spring/dao-applicationContext.xml"}); + } + + /** + * 私有构造函数,带有文件的classpath路径,可能是非applicationContext.xml文件 + */ + private BeanFactoryUtil(String fileClassPath) + { + specialAC = new ClassPathXmlApplicationContext("classpath:" + + fileClassPath); + } + + /** + * 非web.xml方式加载spring配置文件方式的实体实例获取方式 + * @param fileClassPath + * @param beanName + * @return + */ + public synchronized static Object getBeanByClassPathAndBeanName( + String fileClassPath, String beanName) + { + ApplicationContext ac = beanMap.get(fileClassPath); + if (null == ac) + { + ac = new ClassPathXmlApplicationContext("classpath:" + + fileClassPath); + beanMap.put(fileClassPath, ac); + } + return ac.getBean(beanName); + } + + /** + * 获取类实例 + * 默认加载UI组件WEB-INF目录下的applicationContext.xml配置文件 + * @return + * + */ + public synchronized static BeanFactoryUtil getInstance() + { + if (null == defaultBeanFactory) + { + defaultBeanFactory = new BeanFactoryUtil(); + } + return defaultBeanFactory; + } + + /** + * 获取类实例,这种情况一定是在依赖其他组件时没有在applicationContext.xml加载器spring文件时使用 + * 这种情况请少用 + * @param fileClassPath + * @return + */ + @Deprecated + public synchronized static BeanFactoryUtil getInstance(String fileClassPath) + { + if (null == specialBeanFactory) + { + specialBeanFactory = new BeanFactoryUtil(fileClassPath); + } + return specialBeanFactory; + } + + /** + * 获取UI组件WEB-INF目录下的applicationContext.xml配置文件中配置的bean实例 + * @param beanName + * @return + */ + public Object getBean(String beanName) + { + return defaultAC.getBean(beanName); + } + + /** + * 获取没有在applicationContext.xml配置文件中引入的spring配置文件,即没有用容器加载过的配置文件 + * 这里为特殊情况下使用,不推荐使用 + * 推荐在applicationContext.xml配置文件中引入需要使用的spring配置文件,然后使用BeanFactoryUtil.getInstance().getBean("")方法 + * @param beanName + * @return + */ + @Deprecated + public Object getSpecialBean(String beanName) + { + return specialAC.getBean(beanName); + } +} diff --git a/src/com/jsh/util/ExceptionCodeConstants.java b/src/com/jsh/util/ExceptionCodeConstants.java new file mode 100644 index 000000000..36de1fab1 --- /dev/null +++ b/src/com/jsh/util/ExceptionCodeConstants.java @@ -0,0 +1,35 @@ +package com.jsh.util; + +public interface ExceptionCodeConstants +{ + /** + * 用户错误码定义 + */ + public class UserExceptionCode + { + /** + * 用户不存在 + */ + public static final int USER_NOT_EXIST = 1; + + /** + * 用户密码错误 + */ + public static final int USER_PASSWORD_ERROR = 2; + + /** + * 被加入黑名单 + */ + public static final int BLACK_USER = 3; + + /** + * 可以登录 + */ + public static final int USER_CONDITION_FIT = 4; + + /** + * 访问数据库异常 + */ + public static final int USER_ACCESS_EXCEPTION = 5; + } +} diff --git a/src/com/jsh/util/JshConstants.java b/src/com/jsh/util/JshConstants.java new file mode 100644 index 000000000..a4a31dd1f --- /dev/null +++ b/src/com/jsh/util/JshConstants.java @@ -0,0 +1,36 @@ +package com.jsh.util; + +public interface JshConstants +{ + /** + * 定义资产管理公共常量 + * @author jishenghua + */ + public class Common + { + /** + * Info级别日志前缀 + */ + public static final String LOG_INFO_PREFIX = "=========="; + + /** + * error级别日志前缀 + */ + public static final String LOG_ERROR_PREFIX = ">>>>>>>>>>"; + + /** + * debug级别日志前缀 + */ + public static final String LOG_DEBUG_PREFIX = "-----------"; + + /** + * fatal级别日志前缀 + */ + public static final String LOG_FATAL_PREFIX = "$$$$$$$$$$"; + + /** + * warn级别日志前缀 + */ + public static final String LOG_WARN_PREFIX = "##########"; + } +} diff --git a/src/com/jsh/util/JshException.java b/src/com/jsh/util/JshException.java new file mode 100644 index 000000000..ad6438689 --- /dev/null +++ b/src/com/jsh/util/JshException.java @@ -0,0 +1,74 @@ +package com.jsh.util; + +/** + * @title: 平台异常基类 + * @description: 用于包装一些异常信息,打印日志等服务 + * @author jishenghua + * @qq 7 5 2 7 1 8 9 2 0 + * @since: 2014-02-24 + */ +@SuppressWarnings("serial") +public class JshException extends Exception +{ + public long errorCode = -1; + + public String message ; + + public JshException() + { + super(); + } + + public JshException(String message) + { + super(message); + this.message = message; + } + + public JshException(String message, Throwable cause) + { + super(message, cause); + this.message = message; + } + + public JshException(Throwable cause) + { + super(cause); + } + + public JshException(long errorCode) + { + super(); + this.errorCode = errorCode; + } + + public JshException(String message, long errorCode) + { + super(message); + this.errorCode = errorCode; + this.message = message; + } + + public JshException(String message, long errorCode, Throwable cause) + { + super(message, cause); + this.errorCode = errorCode; + this.message = message; + } + + public JshException(long errorCode, Throwable cause) + { + super(cause); + this.errorCode = errorCode; + } + + public long getErrorCode() + { + return errorCode; + } + + public String getMessage() + { + return message; + } +} diff --git a/src/com/jsh/util/OpenSessionInViewFilterExtend.java b/src/com/jsh/util/OpenSessionInViewFilterExtend.java new file mode 100644 index 000000000..daec6104c --- /dev/null +++ b/src/com/jsh/util/OpenSessionInViewFilterExtend.java @@ -0,0 +1,24 @@ +package com.jsh.util; + +import org.hibernate.FlushMode; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.springframework.dao.DataAccessResourceFailureException; +import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter; + +public class OpenSessionInViewFilterExtend extends OpenSessionInViewFilter +{ + @Override + protected Session getSession(SessionFactory sessionFactory) + throws DataAccessResourceFailureException + { + this.setFlushMode(FlushMode.AUTO); + return super.getSession(sessionFactory); + } + @Override + protected void closeSession(Session session, SessionFactory sessionFactory) + { + session.flush(); + super.closeSession(session, sessionFactory); + } +} diff --git a/src/com/jsh/util/PageUtil.java b/src/com/jsh/util/PageUtil.java new file mode 100644 index 000000000..17a298541 --- /dev/null +++ b/src/com/jsh/util/PageUtil.java @@ -0,0 +1,199 @@ +package com.jsh.util; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; +/** + * 分页工具类,实现分页功能 + * @author jishenghua + * @qq 7 5 2 7 1 8 9 2 0 + * @version [版本号version01, 2014-2-21] + */ +@SuppressWarnings("serial") +public class PageUtil implements Serializable +{ + /** + * 总页数,根据总数和单页显示个数进行计算 + */ + private int totalPage = 0; + + /** + * 总个数 + */ + private int totalCount = 0 ; + + /** + * 当前页码 + */ + private int curPage = 1; + + /** + * 每页显示个数 + */ + private int pageSize = 10; + + /** + * 是否为第一页 + */ + private boolean isFirstPage = false; + /** + * 是否是最后一页 + */ + private boolean isLastPage = false; + + /** + * 是否有上一页 + */ + private boolean hasPrevious = false; + + /** + * 是否有下一页 + */ + private boolean hasNext = false; + + /** + * 返回页面list数组 + */ + private List pageList = new ArrayList(); + + /** + * 页面搜索条件,用map来实现 + */ + private Map advSearch = new Hashtable(); + + public PageUtil() + { + + } + + public PageUtil(int totalCount,int pageSize,int curPage,Map adv) + { + init(totalCount,pageSize,curPage,adv); + } + /** + * 初始化页面显示参数 + * @param totalCount 总数 + * @param pageSize 页面显示个数 + * @param curPage 当前页面 + */ + public void init(int totalCount,int pageSize,int curPage,Map adv) + { + this.totalCount = totalCount; + this.pageSize = pageSize ; + this.curPage = curPage; + this.advSearch = adv; + //计算总页数 + if(pageSize != 0) + { + this.totalPage = (totalCount+pageSize-1)/pageSize; + } + if(curPage <1) + { + this.curPage = 1; + } + if(curPage>this.totalPage) + { + this.curPage = this.totalPage; + } + if(curPage>0&&this.totalPage!=1&&curPage0&&this.totalPage!=1&&curPage>1&&curPage<=this.totalPage) + { + this.hasPrevious = true; + } + if(curPage == 1) + { + this.isFirstPage = true ; + } + if(curPage == this.totalPage) + { + this.isLastPage = true; + } + } + public int getTotalPage() + { + return totalPage; + } + public void setTotalPage(int totalPage) + { + this.totalPage = totalPage; + } + public int getTotalCount() + { + return totalCount; + } + public void setTotalCount(int totalCount) + { + this.totalCount = totalCount; + } + public int getCurPage() + { + return curPage; + } + public void setCurPage(int curPage) + { + this.curPage = curPage; + } + public int getPageSize() + { + return pageSize; + } + public void setPageSize(int pageSize) + { + this.pageSize = pageSize; + } + public boolean isFirstPage() + { + return isFirstPage; + } + public void setFirstPage(boolean isFirstPage) + { + this.isFirstPage = isFirstPage; + } + public boolean isLastPage() + { + return isLastPage; + } + public void setLastPage(boolean isLastPage) + { + this.isLastPage = isLastPage; + } + public boolean isHasPrevious() + { + return hasPrevious; + } + public void setHasPrevious(boolean hasPrevious) + { + this.hasPrevious = hasPrevious; + } + public boolean isHasNext() + { + return hasNext; + } + public void setHasNext(boolean hasNext) + { + this.hasNext = hasNext; + } + + public List getPageList() + { + return pageList; + } + public void setPageList(List pageList) + { + this.pageList = pageList; + } + + public Map getAdvSearch() + { + return advSearch; + } + public void setAdvSearch(Map advSearch) + { + this.advSearch = advSearch; + } +} diff --git a/src/com/jsh/util/PathTool.java b/src/com/jsh/util/PathTool.java new file mode 100644 index 000000000..b3e1335a9 --- /dev/null +++ b/src/com/jsh/util/PathTool.java @@ -0,0 +1,85 @@ +package com.jsh.util; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; + +import com.jsh.base.Log; + +/** + * 获取应用系统路径 + * @author jishenghua + * @qq 7 5 2 7 1 8 9 2 0 + */ +public class PathTool +{ + + /** + * 获取WEB-INF的绝对路径 + * @return + */ + public static String getWebinfPath() + { + String webinfPath = ""; + //获取URL对象 + URL url = PathTool.class.getClassLoader().getResource(""); + try + { + //获取路径 + webinfPath = url.toURI().getPath(); + //截取路径到WEB-INF结束 +// webinfPath = path.substring(0, path.indexOf("/WEB-INF") + 8); + } + catch (URISyntaxException e) + { + Log.errorFileSync(">>>>>>>>>>>>>>>>>>>>>>>路径获取异常", e); + } + return webinfPath; + } + + /** + * 获取webapp的绝对路径 + * @return + */ + public static String getWebappPath() + { + //先获取工程路径 + String projectPath = getProjectPath(); + //获取工程路径的上级路径 + File f = new File(projectPath); + //路径不存在就返回 + if (!f.exists()) + { + return projectPath; + } + else + { + //返回webapp路径 + return f.getParent(); + } + } + + /** + * 获取工程的绝对路径 + * @return + */ + public static String getProjectPath() + { + String projectPath = ""; + //获取URL对象 + URL url = PathTool.class.getClassLoader().getResource(""); + String path = null; + try + { + //获取路径 + path = url.toURI().getPath(); + //截取webapp路径 + projectPath = path.substring(0, path.indexOf("/WEB-INF")); + } + catch (URISyntaxException e) + { + Log.errorFileSync(">>>>>>>>>>>>>>>>>>>>>>>路径获取异常", e); + } + return projectPath; + } +} diff --git a/src/com/jsh/util/SearchConditionUtil.java b/src/com/jsh/util/SearchConditionUtil.java new file mode 100644 index 000000000..b2bf40a9a --- /dev/null +++ b/src/com/jsh/util/SearchConditionUtil.java @@ -0,0 +1,127 @@ +package com.jsh.util; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * 根据搜索条件拼装成查询hql语句 + * @author jishenghua qq:752718920 + */ +public class SearchConditionUtil +{ + //拼接字符串的前缀空格字符串 + private static final String emptyPrefix = " and "; + + /** + * 根据搜索条件自动拼接成hql搜索语句 + * @param condition 搜索条件 规则: + * 1、类型 n--数字 s--字符串 + * 2、属性 eq--等于 neq--不等于 like--像'%XX%' llike--左像'%XX' rlike--右像'XX%' in--包含 gt--大于 gteq--大于等于 lt--小于 lteq--小于等于 + * order--value desc asc gy-- group by + * 示例: + * Map condition = new HashMap(); + * condition.put("supplier_s_like", "aaa"); + * condition.put("contacts_s_llike", "186"); + * condition.put("contacts_s_rlike", "186"); + * condition.put("phonenum_s_eq", null); + * condition.put("email_n_neq", 23); + * condition.put("description_s_order", "desc"); + * @return 封装后的字符串 + */ + public static String getCondition(Map condition) + { + StringBuffer hql = new StringBuffer(); + Set key = condition.keySet(); + String groupbyInfo = ""; + String orderInfo = ""; + for(String keyInfo:key) + { + /* + * 1、数组为三个 第一个为对象实例的字段 第二个为字段类型 第三个为属性 + * 2、根据分解后的数组拼接搜索条件 + */ + Object valueInfo = condition.get(keyInfo); + if(null != valueInfo &&valueInfo.toString().length()>0) + { + String[] searchCondition = keyInfo.split("_"); + if(searchCondition[1].equals("n")) + hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + valueInfo); + else if(searchCondition[1].equals("s")) + { + if(searchCondition[2].equals("like")) + hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'%" + valueInfo + "%'"); + else if(searchCondition[2].equals("llike")) + hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'%" + valueInfo + "'"); + else if(searchCondition[2].equals("rlike")) + hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'" + valueInfo + "%'"); + else if(searchCondition[2].equals("in")) + hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "(" + valueInfo + ")"); + else if(searchCondition[2].equals("order")) + orderInfo = " order by " + searchCondition[0] + " " + valueInfo; + else if(searchCondition[2].equals("gb")) + groupbyInfo = " group by " + searchCondition[0]; + else + hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'" + valueInfo + "'"); + } + } + } + return hql.append(groupbyInfo).append(orderInfo).toString(); + } + + /** + * 获取指定类型的符号 + * 属性 eq--等于 neq--不等于 like--像 in--包含 gt--大于 gteq--大于等于 lt--小于 lteq--小于等于 order--value desc asc + * @param type + * @return 类型字符串 + */ + private static String getType(String type) + { + String typeStr = ""; + if(type.equals("eq")) + typeStr = " = "; + else if(type.equals("neq")) + typeStr = " != "; + else if(type.equals("like")) + typeStr = " like "; + else if(type.equals("llike")) + typeStr = " like "; + else if(type.equals("rlike")) + typeStr = " like "; + else if(type.equals("in")) + typeStr = " in "; + else if(type.equals("gt")) + typeStr = " > "; + else if(type.equals("gteq")) + typeStr = " >= "; + else if(type.equals("lt")) + typeStr = " < "; + else if(type.equals("lteq")) + typeStr = " <= "; + else if(type.equals("order")) + typeStr = " order "; + else if(type.equals("gy")) + typeStr = " group by "; + else + typeStr = "unknown"; + return typeStr; + } + + public static void main(String[] args) + { + /** + * 拼接搜索条件 + */ + Map condition = new HashMap(); + condition.put("supplier_s_like", "aaa"); + condition.put("contacts_s_llike", "186"); + condition.put("contacts_s_rlike", "186"); + condition.put("phonenum_s_eq", null); + condition.put("email_n_neq", 23); + condition.put("description_s_order", "desc"); + condition.put("description_s_gb", "aaa"); + + //获取搜索条件拼接 + System.out.println(getCondition(condition)); + } +} diff --git a/src/com/jsh/util/SessionFilter.java b/src/com/jsh/util/SessionFilter.java new file mode 100644 index 000000000..762a3c320 --- /dev/null +++ b/src/com/jsh/util/SessionFilter.java @@ -0,0 +1,70 @@ +package com.jsh.util; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +/** + * 用户登录session处理类 + * 过滤session是否超时 + * @author jishenghua qq_752718920 + * @version [版本号, 2012-3-6] + * @see [相关类/方法] + * @since + */ +public class SessionFilter implements Filter +{ + /** + * 初始化过滤器 暂不处理 + * 重载方法 + * @param arg0 + * @throws ServletException + */ + + public void init(FilterConfig arg0) + throws ServletException + { + + } + /** + * 判断用户session是否存在 不存在则跳转到登录页面 + * 重载方法 + * @param srequest + * @param sresponse + * @param chain + * @throws IOException + * @throws ServletException + */ + public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain) + throws IOException, ServletException + { + HttpServletRequest request = (HttpServletRequest) srequest; + HttpServletResponse response = (HttpServletResponse) sresponse; + HttpSession session = request.getSession(); + + //获取工程路径 + String path = request.getContextPath(); + String requestURl = request.getRequestURI(); + + if(requestURl.contains("/pages") &&null != session.getAttribute("user")) + chain.doFilter(request, response); + else + response.sendRedirect(path + "/logout.jsp"); + } + + /** + * 销毁过滤器 + */ + public void destroy() + { + + } +} diff --git a/src/com/jsh/util/Tools.java b/src/com/jsh/util/Tools.java new file mode 100644 index 000000000..68e7daea5 --- /dev/null +++ b/src/com/jsh/util/Tools.java @@ -0,0 +1,561 @@ +package com.jsh.util; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.InetAddress; +import java.net.URLEncoder; +import java.net.UnknownHostException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.UUID; +import java.util.regex.Pattern; +import java.math.BigInteger; +/** + * 工具类 + * @author jishenghua qq:7-5-2-7-1-8-9-2-0 + */ +public class Tools +{ + /** + * 获得32位唯一序列号 + * @return 32为ID字符串 + */ + public static String getUUID_32() + { + return UUID.randomUUID().toString().replaceAll("-", ""); + } + /** + * 获得当天时间,格式为yyyy-MM-dd + * @return 格式化后的日期格式 + */ + public static String getNow() + { + return new SimpleDateFormat("yyyy-MM-dd").format(new Date()); + } + + /** + * 获取当前月 yyyy-MM + * @return + */ + public static String getCurrentMonth() + { + return new SimpleDateFormat("yyyy-MM").format(new Date()); + } + + /** + * 获取指定日期格式 yyyy-MM + * @return + */ + public static String getCurrentMonth(Date date) + { + return new SimpleDateFormat("yyyy-MM-dd").format(date); + } + + /** + * 获得当天时间,格式为yyyyMMddHHmmss + * @return 格式化后的日期格式 + */ + public static String getNow2(Date date) + { + return new SimpleDateFormat("yyyyMMddHHmmss").format(date); + } + /** + * 获得当天时间,格式为yyyy-MM-dd HH:mm:ss + * @return 格式化后的日期格式 + */ + public static String getNow3() + { + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); + } + /** + * 获得指定时间,格式为yyyy-MM-dd HH:mm:ss + * @return 格式化后的日期格式 + */ + public static String getCenternTime(Date date) + { + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); + } + + /** + * 获得指定时间,格式为mm:ss + * @return 格式化后的日期格式 + */ + public static String getTimeInfo(Date date) + { + return new SimpleDateFormat("mm:ss").format(date); + } + /** + * 获取当前日期是星期几 + * return 星期几 + */ + public static String getWeekDay() + { + Calendar c = Calendar.getInstance(Locale.CHINA); + c.setTime(new Date()); + int day=c.get(Calendar.DAY_OF_WEEK); + String weekDay = ""; + switch (day) + { + case 1: + weekDay = "星期日"; + break; + case 2: + weekDay = "星期一"; + break; + case 3: + weekDay = "星期二"; + break; + case 4: + weekDay = "星期三"; + break; + case 5: + weekDay = "星期四"; + break; + case 6: + weekDay = "星期五"; + break; + case 7: + weekDay = "星期六"; + break; + default: + break; + } + return weekDay; + } + /** + * 判断字符串是否全部为数字 + * @param accountWaste + * @return boolean值 + */ + public static boolean checkStrIsNum(String checkStr) + { + if(checkStr == null || checkStr.length() ==0) + return false; + return Pattern.compile("^[0-9]*.{1}[0-9]*$").matcher(checkStr).matches(); +// return Pattern.compile(":^[0-9]+(.[0-9])*$").matcher(checkStr).matches(); + } + /** + * 获得前一天的时间 + * @return 前一天日期 + */ + public static String getPreviousDate() + { + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.DATE, -1); + return new SimpleDateFormat("yyyy-MM").format(cal.getTime()); + } + + /** + * 截取字符串长度 + * @param beforeStr + * @param cutLeng + * @return 截取后的字符串 + */ + public static String subStr(String beforeStr,int cutLeng){ + if(beforeStr.length()>cutLeng) + return beforeStr.substring(0,cutLeng)+ "..." ; + return beforeStr ; + } + /** + * 生成随机字符串,字母和数字混合 + * @return 组合后的字符串 ^[0-9a-zA-Z] + */ + public static String getRandomChar(){ + //生成一个0、1、2的随机数字 + int rand = (int)Math.round(Math.random() * 1); + long itmp = 0; + char ctmp = '\u0000'; + switch (rand) + { + //生成大写字母 + 1000以内数字 + case 1: + itmp = Math.round(Math.random() * 25 + 65); + ctmp = (char)itmp; + return String.valueOf(ctmp) + (int)Math.random()*1000; + //生成小写字母 + case 2: + itmp = Math.round(Math.random() * 25 + 97); + ctmp = (char)itmp; + return String.valueOf(ctmp)+ (int)Math.random()*1000; + //生成数字 + default : + itmp = Math.round(Math.random() * 1000); + return itmp + ""; + } + } + + /** + * 判断首字母以数字开头,字符串包括数字、字母%以及空格 + * @param str 检查字符串 + * @return 是否以数字开头 + */ + public static boolean CheckIsStartWithNum(String str) + { + return Pattern.compile("^[0-9][a-zA-Z0-9%,\\s]*$").matcher(str).matches(); + } + /** + * 判断首字母以","开头,字符串包括数字、字母%以及空格 + * @param str 检查字符串 + * @return 是否以数字开头 + */ + public static boolean CheckIsStartWithSpec(String str) + { + return Pattern.compile("^[,][a-zA-Z0-9%,\\s]*$").matcher(str).matches(); + } + /** + * 字符转码 + * @param aValue + * @return + * @see 转码后的字符串 + */ + public static String encodeValue(String aValue) + { + if(aValue.trim().length() ==0) + { + return ""; + } + String valueAfterTransCode = null; + try + { + valueAfterTransCode = URLEncoder.encode(aValue, "UTF-8"); + } + catch (UnsupportedEncodingException e) + { + e.getMessage(); + } + return valueAfterTransCode; + } + /** + * 去除str中的' + * @param str + * @return 除去'后的字符串 + * @see [类、类#方法、类#成员] + */ + public static String afterDealStr(String str) + { + return str.replace("'", ""); + } + /** + * 获取用户IP地址 + * @return 用户IP + * @see [类、类#方法、类#成员] + */ + public static String getCurrentUserIP() + { + try + { + return InetAddress.getLocalHost().getHostAddress(); + } + catch (UnknownHostException e) + { + e.printStackTrace(); + return "127.0.0.1"; + } + } + + /** + * 转化前台批量传入的ID值 + * @param data + * @return 转化后的ID值数组 + */ + public static int[] changeDataForm(String data) + { + String[] dataStr = data.split(","); + int[] dataInt = new int[dataStr.length]; + for(int i = 0 ;i < dataStr.length;i ++) + dataInt[i] = Integer.parseInt(dataStr[i]); + return dataInt; + } + + /** + * 解决导出文件中文乱码问题firefox和ie下中文乱码 + */ + public static String changeUnicode(String fileName,String browserType) + { + String returnFileName = ""; + try + { + if(browserType.equalsIgnoreCase("MSIE")) + { + returnFileName = URLEncoder.encode(fileName, "ISO8859-1"); + returnFileName = returnFileName.replace(" ","%20"); + if (returnFileName.length() > 150) + { + returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1"); + returnFileName = returnFileName.replace(" ", "%20"); + } + } + else if(browserType.equalsIgnoreCase("Firefox")) + { + returnFileName = new String(fileName.getBytes("ISO8859-1"),"ISO8859-1"); + returnFileName = returnFileName.replace(" ", "%20"); + } + else + { + returnFileName = URLEncoder.encode(fileName, "ISO8859-1"); + returnFileName = returnFileName.replace(" ","%20"); + if (returnFileName.length() > 150) + { + + returnFileName = new String(returnFileName.getBytes("GB2312"), "ISO8859-1"); + returnFileName = returnFileName.replace(" ", "%20"); + } + } + } + catch (UnsupportedEncodingException e) + { + e.printStackTrace(); + } + return returnFileName; + } + + /** + * 写理财日志内容转化特殊字符 + * @param str 需要转化的字符 + * @return 转化后的字符 + */ + public static String htmlspecialchars(String str) + { + str = str.replaceAll("&", "&"); + str = str.replaceAll("<", "<"); + str = str.replaceAll(">", ">"); + str = str.replaceAll("\"", """); + return str; + } + + /** + * 根据消费日期获取消费月 + * @param consumeDate 消费日期 + * @return 返回消费月信息 + */ + public static String getConsumeMonth(String consumeDate) + { + return consumeDate.substring(0,7); + } + + /** + * 获取当前日期的前XX个月 + * @param 之前的第几个月 + * @return 前XX个月字符串 + */ + public static String getBeforeMonth(int beforeMonth) + { + Calendar c = Calendar.getInstance(); + c.add(Calendar.MONTH, -beforeMonth); + return new SimpleDateFormat("yyyy-MM").format(c.getTime()); + } + + + /** + * 获取email用户姓名 + * @param args + */ + public static String getEmailUserName(String emailAddress) + { + return emailAddress.substring(0,emailAddress.lastIndexOf("@")); + } + + /** + * 获取中文编码,邮件附件乱码问题解决 + * @param str + * @return + */ + public static String getChineseString(String emailAttchmentTitle) + { + if(emailAttchmentTitle!=null&&!emailAttchmentTitle.equals("")) + { + try + { + return new String(emailAttchmentTitle.getBytes(),"ISO-8859-1"); + } + catch (UnsupportedEncodingException e) + { + e.printStackTrace(); + } + } + return emailAttchmentTitle; + } + + /** + * 判断userTel是否合法,userTel只能是数字 + * @param userTel + * @return true 合法 false不合法 + */ + public static boolean isTelNumber(String userTel) + { + String reg_phone="^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}$"; + String reg_tel="^(1[0-9][0-9]|1[0-9][0|3|6|8|9])\\d{8}$"; + boolean b_phpne=Pattern.compile(reg_phone).matcher(userTel).matches(); + boolean b_tel=Pattern.compile(reg_tel).matcher(userTel).matches(); + return (b_phpne || b_tel); + } + + /** + * 模糊判断电话号码是否合法,只能是数字 + * @param macAddress + * @return + */ + public static boolean isTelNumberBySlur(String userTel) + { + return Pattern.compile("^([\\s0-9]{0,12}$)").matcher(userTel).matches(); + } + + /** + * 获取当前时间的字符串类型 + * @return 处理后的字符串类型 + */ + public static String getNowTime() + { + return new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime()); + } + + /** + * 开打指定文件 + * @param filePath 文件的绝对路径 + */ + public static void openFile(String filePath) + { + String viewFilePath = filePath.replace("\\", "/"); + // Runtime.getRuntime().exec("cmd /c start "+filePath); + // 解决路径中带空格问题 + Runtime r = Runtime.getRuntime(); + String[] cmdArray = new String[] { "cmd.exe", "/c", viewFilePath }; + try + { + r.exec(cmdArray); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + /** + * 判断字符串中是否含有中文 + * @author jishenghua + * @param str + * @return + */ + public static boolean isContainsChinese(String str) + { + return Pattern.compile("[\u4e00-\u9fa5]").matcher(str).matches(); + } + + /** + * 过滤html文件中的文本 + * @param content + * @return过滤后的文本 + */ + public static String filterText(String content) + { + return content.replace("/<(?:.|\\s)*?>/g",""); + } + + /** + * 去掉字符串中所有符号,不论是全角,还是半角的,或是货币符号或者空格等 + * @author jishenghua + * @param s + * @return + * + */ + public static String removeSymbolForString(String s) + { + StringBuffer buffer = new StringBuffer(); + char[] chars = s.toCharArray(); + for (int i = 0; i < chars.length; i++) + { + if ((chars[i] >= 19968 && chars[i] <= 40869) || (chars[i] >= 97 && chars[i] <= 122) || (chars[i] >= 65 && chars[i] <= 90)) + { + buffer.append(chars[i]); + } + } + return buffer.toString(); + } + + /** + * 获取一个字符串的MD5 + * @param msg + * @return 加密后的MD5字符串 + * @throws NoSuchAlgorithmException + */ + public static String md5Encryp(String msg) throws NoSuchAlgorithmException + { + // 生成一个MD5加密计算摘要 + MessageDigest md = MessageDigest.getInstance("MD5"); + // 计算md5函数 + md.update(msg.getBytes()); + return new BigInteger(1, md.digest()).toString(16); + } + + /** + * 处理字符串null值 + * @param beforeStr 处理前字符串 + * @return 处理后的字符串 + */ + public static String dealNullStr(String beforeStr) + { + if(null == beforeStr || beforeStr.length()==0) + return ""; + return beforeStr; + } + + /** + * 使用参数Format将字符串转为Date + * @author jishenghua + * @param strDate + * @param pattern + * @return + * @throws ParseException + * + */ + public static Date parse(String strDate, String pattern) + throws ParseException + { + return new SimpleDateFormat(pattern).parse(strDate); + } + +// /** +// * 过滤html文件中的图片文件 +// * @param content +// * @return +// */ +// public static String filterImg(String content) +// { +// return content.matches("//g"); +// } + + public static void main(String[] args) + { + String aa = "的付的反对法的发的说法"; + char[] bb = aa.toCharArray(); + for(char c : bb) + { + System.out.println(c); + } + System.out.println(getBeforeMonth(1)); + + try + { + System.out.println(md5Encryp("guest")); + System.out.println(md5Encryp("admin")); + } + catch (NoSuchAlgorithmException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + String value = "2333"; + System.out.println(checkStrIsNum(value)); + + for(int i = 0 ;i < 100;i ++) + { + System.out.print(getRandomChar() + " || "); + } + } +}