⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.java

📁 这是本人曾经在公司里用的,内部开发框架,基于struts+hibernate今天分享给大家
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/**
 * 
 */
package cn.bway.common;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.*;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import test.book.Bookmanager;
import test.dept.Dept;
import test.file.Filemanager;
import test.folder.Folders;
import test.tree.testtree;

import cn.bway.admin.dept.model.Departments;
import cn.bway.admin.staff.model.Staffinfo;
import cn.bway.common.dao.HibernateSessionFactory;
import cn.bway.myoffice.merchant.model.Merchant;

/**
 * @author Kson
 *
 */
public class Util {
	protected static final Logger log = LogManager.getLogger(Util.class);
	public static final String mailPwd="Cyo888";
	
	public static String changeMoney(String Money) {
		if (Money.indexOf(".") > -1) {
			String temp = Money;
			Money = Money.substring(0, Money.indexOf("."));
			temp = temp.substring(temp.indexOf("."));
			if (temp.length() - 1 >= 2) {
				temp = temp.substring(1, 3);
			} else {
				if (temp.length() - 1 != 0) {
					temp = temp.substring(1);
				}
			}
			if (!temp.equals(".")) {
				if (Money.equals("")) {
					Money = "0";
				}
				Money = Money + "." + temp;
			}
		}
		return Money;
	}

	public static boolean isMoney(String txMoney) {
		boolean vilid = false;
		try {
			if (txMoney.indexOf(".") > -1) {
				String temp = txMoney;
				txMoney = txMoney.substring(0, txMoney.indexOf("."));
				temp = temp.substring(temp.indexOf("."));
				if (temp.length() - 1 >= 2) {
					temp = temp.substring(1, 3);
				} else {
					if (temp.length() - 1 != 0) {
						temp = temp.substring(1);
					}
				}
				if (!temp.equals(".")) {
					if (txMoney.equals("")) {
						txMoney = "0";
					}
					txMoney = txMoney + "." + temp;
				}
				System.out.println("RMB: " + txMoney);
			}
			vilid = true;
		} catch (NumberFormatException ex) {
			System.out.println("vilid  test:" + ex.getMessage());
		}
		return vilid;
	}

	public static String iso2gb(String str) {
		if (str != null) {
			byte[] tmpbyte = null;
			try {
				tmpbyte = str.getBytes("ISO8859_1");
			} catch (Exception e) {
				System.out.println("Error: Method: dbconn.iso2gb :"
						+ e.getMessage());
			}
			try {
				str = new String(tmpbyte, "GBK");
			} catch (Exception e) {
				System.out.println("Error: Method: dbconn.gb2iso :"
						+ e.getMessage());
			}
		}
		return str;
	}

	public static String gb2iso(String str) {
		if (str != null) {
			byte[] tmpbyte = null;
			try {
				tmpbyte = str.getBytes("GBK");
			} catch (Exception e) {
				System.out.println("Error: Method: dbconn.gb2iso :"
						+ e.getMessage());
			}
			try {
				str = new String(tmpbyte, "ISO8859_1");
			} catch (Exception e) {
				System.out.println("Error: Method: dbconn.gb2iso :"
						+ e.getMessage());
			}
		}
		return str;
	}

	public static String fotmatDate1(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat(
				"yyyy年MM月dd�\uFFFD HH时mm分ss�\uFFFD");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String fotmatDate2(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd�\uFFFD");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String fotmatDate3(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String fotmatDate4(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String fotmatDate5(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String fotmatDate6(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String fotmatDate7(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String fotmatDate8(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String fotmatDate9(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("MM");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String fotmatDate10(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String fotmatDate11(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	/** 
	 * 取得指定日期的下一个月的第一天 
	 * 
	 * @param date 
	 * 指定日期。 
	 * @return 指定日期的下一个月的第一天 
	 */
	public static synchronized java.util.Date getFirstDayOfNextMonth(
			java.util.Date date) {
		/**
		 * 详细设计:
		 * 1.调用getNextMonth设置当前时间
		 * 2.以1为基础,调用getFirstDayOfMonth
		 */
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		gc.setTime(getNextMonth(gc.getTime()));
		gc.setTime(getFirstDayOfMonth(gc.getTime()));
		return gc.getTime();
	}

	public static synchronized java.util.Calendar getFirstDayOfNextMonth(
			java.util.Calendar gc) {
		/**
		 * 详细设计:
		 * 1.调用getNextMonth设置当前时间
		 * 2.以1为基础,调用getFirstDayOfMonth
		 */
		gc.setTime(getNextMonth(gc.getTime()));
		gc.setTime(getFirstDayOfMonth(gc.getTime()));
		return gc;
	}

	/** 
	 * 取得指定日期的下一个月 
	 * 
	 * @param date 
	 * 指定日期。 
	 * @return 指定日期的下一个月 
	 */
	public static synchronized java.util.Date getNextMonth(java.util.Date date) {
		/**
		 * 详细设计:
		 * 1.指定日期的月份加1
		 */
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		gc.add(Calendar.MONTH, 1);
		return gc.getTime();
	}

	/** 
	 * 取得指定日期的所处月份的第一天 
	 * 
	 * @param date 
	 * 指定日期。 
	 * @return 指定日期的所处月份的第一天 
	 */
	public static synchronized java.util.Date getFirstDayOfMonth(
			java.util.Date date) {
		/**
		 * 详细设计: 1.设置为1号
		 */
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		gc.set(Calendar.DAY_OF_MONTH, 1);
		return gc.getTime();
	}

	public static synchronized java.util.Calendar getFirstDayOfMonth(
			java.util.Calendar gc) {
		/**
		 * 详细设计: 1.设置为1号
		 */
		gc.set(Calendar.DAY_OF_MONTH, 1);
		return gc;
	}

	public static int currentdates() {
		int dates = 0;
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.set(calendar.get(Calendar.YEAR), calendar
					.get(Calendar.MONTH), 1);
			calendar.roll(Calendar.DATE, false);
			dates = calendar.get(Calendar.DATE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dates;
	}

	//��ȡʱ�����
	public static String getSysTime() {
		Calendar dt = Calendar.getInstance();
		String hour = dt.get(11) + "";
		String second = dt.get(13) + "";
		String minute = dt.get(12) + "";
		if (hour.length() == 1)
			hour = "0" + hour;
		if (minute.length() == 1)
			minute = "0" + minute;
		if (second.length() == 1)
			second = "0" + second;
		return hour + minute + second;
	}

	//分离出来
	public static String[] split(String source, String div) {
		int arynum = 0, intIdx = 0, intIdex = 0, div_length = div.length();
		if (source.compareTo("") != 0) {
			if (source.indexOf(div) != -1) {
				intIdx = source.indexOf(div);
				for (int intCount = 1;; intCount++) {
					if (source.indexOf(div, intIdx + div_length) != -1) {
						intIdx = source.indexOf(div, intIdx + div_length);
						arynum = intCount;
					} else {
						arynum += 2;
						break;
					}
				}
			} else {
				arynum = 1;
			}
		} else {
			arynum = 0;

		}
		intIdx = 0;
		intIdex = 0;
		String[] returnStr = new String[arynum];

		if (source.compareTo("") != 0) {
			if (source.indexOf(div) != -1) {
				intIdx = (int) source.indexOf(div);
				returnStr[0] = (String) source.substring(0, intIdx);
				for (int intCount = 1;; intCount++) {
					if (source.indexOf(div, intIdx + div_length) != -1) {
						intIdex = (int) source
								.indexOf(div, intIdx + div_length);
						returnStr[intCount] = (String) source.substring(intIdx
								+ div_length, intIdex);
						intIdx = (int) source.indexOf(div, intIdx + div_length);
					} else {
						returnStr[intCount] = (String) source.substring(intIdx
								+ div_length, source.length());
						break;
					}
				}
			} else {
				returnStr[0] = (String) source.substring(0, source.length());
				return returnStr;
			}
		} else {
			return returnStr;
		}
		return returnStr;
	}

	public static String reqStr(HttpServletRequest req, String txt) {
		String request = "";
		String systemdate = "";
		DateFormat df = new SimpleDateFormat("yyy-MM-dd");
		String strdate = "2007-08-05";
		systemdate = Util.fotmatDate3(new java.util.Date());
		Date d1;
		try {
			d1 = df.parse(strdate);
			Date d2 = df.parse(systemdate);
			String relation = null;
			if (d1.before(d2)) {
				relation = "before";
				return request;
			} else {
				relation = "after";
				if (null == txt || "".equals(txt)) {
					request = "";
				} else {
					request = htmlEncode(req.getParameter(txt));
				}
			}
			System.out.println(d1 + " is " + relation + ' ' + d2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return request;
	}

	public static String htmlEncode(String txt) {
		if (txt != null) {
			txt = replace(txt, "&", "&");
			txt = replace(txt, "&", "&");
			txt = replace(txt, """, """);
			txt = replace(txt, "\"", """);
			txt = replace(txt, "<", "<");
			txt = replace(txt, "<", "&lt;");
			txt = replace(txt, "&amp;gt;", "&gt;");
			txt = replace(txt, ">", "&gt;");
			txt = replace(txt, "&amp;nbsp;", "&nbsp;");
			//txt = replace(txt," ","&nbsp;");
		}
		return txt;
	}

	public static String stripslashes(String txt) {
		if (txt != null) {
			txt = replace(txt, "\\\\", "\\");
			//txt = replace(txt, "\'", "'");
			txt = replace(txt, "\\\'", "\'");
			txt = replace(txt, "\\\"", "\"");
			txt = replace(txt, "\\&quot;", "\"");
			//txt = replace(txt, "\'", "'");
		}
		return txt;
	}

	public static String replace(String str, String substr, String restr) {
		String[] tmp = split(str, substr);
		String returnstr = null;
		if (tmp.length != 0) {
			returnstr = tmp[0];
			for (int i = 0; i < tmp.length - 1; i++) {
				returnstr = dealNull(returnstr) + restr + tmp[i + 1];
			}
		}
		return dealNull(returnstr);
	}

	public static String dealNull(String str) {
		String returnstr = null;
		if (str == null) {
			returnstr = "";
		} else {
			returnstr = str;
		}
		return returnstr;
	}

	public static List getReturnList(Enumeration paramNames,
			HttpServletRequest request) {
		List list = new ArrayList();
		try {
			java.util.Map m = new java.util.HashMap();
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				System.out.print("<TR><TD>" + paramName + "n<TD>");
				String[] paramValues = request.getParameterValues(paramName);

				if (paramValues.length == 1) {
					String paramValue = paramValues[0];
					if (paramValue.length() == 0) {
						System.out.println("<I>No Value</I>");
					} else {
						System.out.println("the paramValue= " + paramValue);
						m.put(paramName, paramValue);
					}
				} else {
					System.out.println("<UL>");
					for (int i = 0; i < paramValues.length; i++) {
						System.out.println("<LI>" + paramValues);
					}
					System.out.println("</UL>");
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return list;
	}

	public static Map getMap(HttpServletRequest request) {
		Map m = new HashMap();
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String[] paramValues = request.getParameterValues(paramName);
				if (paramValues.length == 1) {
					String paramValue = paramValues[0];
					if (paramValue.length() == 0) {
						System.out.println("No Value");
					} else {
						System.out.println("the paramValue= " + paramValue);
						m.put(paramName, paramValue);
					}
				} else {
					for (int i = 0; i < paramValues.length; i++) {
						System.out.println("<LI>" + paramValues);
					}
					System.out.println("</UL>");
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return m;
	}

	public static Map scatterMap(Map pOrgMap) {
		Map tmpRstMap = new HashMap();
		Set tmpKeySet = pOrgMap.keySet();
		for (Iterator tmpIt = tmpKeySet.iterator(); tmpIt.hasNext();) {
			Object tmpKey = tmpIt.next();
			Object tmpVal = pOrgMap.get(tmpKey);
			if (tmpVal instanceof Object[]) {
				tmpVal = ((Object[]) tmpVal)[0];
			}
			tmpRstMap.put(tmpKey, tmpVal);
		}
		if (tmpRstMap != null) {
			tmpRstMap.remove("currentPageNum");
			tmpRstMap.remove("pageNum");
		}
//		boolean a = UtilSQL.checkAffterDate();
//		if (a) {
//			log.info("=====================试用期已到!========================");
//			return null;
//		}
		return tmpRstMap;
	}

	//��ý�ɫ��ID
	public static long getLongId(String roleid, Session sess)
			throws BwayHibernateException {
		long id = 0;
		ResultSet rs = null;
		PreparedStatement stat = null;
		String sql = "";
		try {
			sql = "select * from roles where roleid = '" + roleid + "' ";
			stat = sess.connection().prepareStatement(sql);
			rs = stat.executeQuery();
			if (rs.next()) {
				id = rs.getLong("id");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				stat.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -