java工具类

日期工具

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtil {

	public final static String DATEFORMATYMD = "yyyy-MM-dd";

	public final static String DATEFORMATHMS = "yyyy-MM-dd HH:mm:ss";
	public final static String DATEFORMATHM = "yyyy-MM-dd HH:mm";

	public final static String DATEFORMATCH = "yyyy年MM月dd日";

	public final static String DATEFORMATMCH = "yyyy年MM月dd日 HH时mm分ss秒";

	public static DateFormat FORMAT_TIME = null;

	public static Calendar c = Calendar.getInstance();;
	public DateUtil(){}

	/**
	 * 得到格式化后的日期
	 * @param date需要格式化的日期
	 * @param marked 标示格式
	 * 			(1:yyyy-MM-dd;
	 * 			2:yyyy-MM-dd HH:mm:ss;
	 * 			3:yyyy年MM月dd日;
	 * 			4:yyyy年MM月dd日 HH时mm分ss秒
	 * 			5:自定义格式)
	 * 			当marked=5时,参数dateFormat为必填项
	 * 			默认状态为2
	 * */
	public static String getDate(Date date, int marked, String dateFormat) {
		switch (marked) {
		case 1:
			FORMAT_TIME = new SimpleDateFormat(DATEFORMATYMD);
			break;
		case 2:
			FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS);
			break;
		case 3:
			FORMAT_TIME = new SimpleDateFormat(DATEFORMATCH);
			break;
		case 4:
			FORMAT_TIME = new SimpleDateFormat(DATEFORMATMCH);
			break;
		case 5:
			FORMAT_TIME = new SimpleDateFormat(dateFormat);
			break;
		case 6:
			FORMAT_TIME = new SimpleDateFormat(DATEFORMATHM);
			break;
		default:
			FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS);
			break;
		}
		if(date==null)
			date=new Date();
		return FORMAT_TIME.format(date);
	}


	/**
	 * 格式化字符串为日期的函数.
	 * 
	 * @param strDate
	 *            字符串.
	 * @param     marked 标示格式
	 * 			(1:yyyy-MM-dd;
	 * 			2:yyyy-MM-dd HH:mm:ss;
	 * 			3:yyyy年MM月dd日;
	 * 			4:yyyy年MM月dd日 HH时mm分ss秒
	 * 			5:自定义格式)
	 * 			当marked=5时,参数dateFormat为必填项
	 * 			默认状态为2
	 * @param format
	 *            转换格式如:"yyyy-MM-dd HH:mm:ss"
	 * @return 字符串包含的日期,失败则返回当前日期
	 */
	public static Date parseDate(String strDate,int marked, String format) {
		try {
			if (strDate == null || strDate.equals("") || strDate.equals("null") || strDate.trim().length() == 0){
				return new Date();
			}
			switch (marked) {
			case 1:
				FORMAT_TIME = new SimpleDateFormat(DATEFORMATYMD);
				break;
			case 2:
				FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS);
				break;
			case 3:
				FORMAT_TIME = new SimpleDateFormat(DATEFORMATCH);
				break;
			case 4:
				FORMAT_TIME = new SimpleDateFormat(DATEFORMATMCH);
				break;
			case 5:
				FORMAT_TIME = new SimpleDateFormat(format);
				break;
			default:
				FORMAT_TIME = new SimpleDateFormat(DATEFORMATHMS);
				break;
			}
			return FORMAT_TIME.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
			return new Date();
		}
	}
	/**
	 * 判断输入的字符串是否为日期格式(2011-04-04 10:36:00)
	 * @param str需要判断的字符串
	 * @return 是返回true,否则返回false
	 * */
	public static boolean isDateTime(String str){
		String regex = "^(((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d)$";
		Pattern pattern = Pattern.compile(regex);
		Matcher m = pattern.matcher(str);
		return m.matches();
	}
	/**
	 * 判断是否为日期(例如2011-04-04)
	 * @param str需要判断的字符串
	 * @return 是返回true,否则返回false
	 * */
	public static boolean isDate(String str){
		String regex = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-9]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
		Pattern pattern = Pattern.compile(regex);
		Matcher m = pattern.matcher(str);
		return m.matches();
	}
	/**
	 * 判断是否为日期(例如10:41:12)
	 * @param str需要判断的字符串
	 * @return 是返回true,否则返回false
	 * */
	public static boolean isTime(String str){
		String regex = "^(20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d)$";
		Pattern pattern = Pattern.compile(regex);
		Matcher m = pattern.matcher(str);
		return m.matches();
	}
}

DBUtil库

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class DBUtil {

	/**
	 * 获取数据库连接对象
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection() throws Exception{
		
		ResourceBundle bundle = ResourceBundle.getBundle("config.properties.JDBC");
		 String DriverName  = bundle.getString("JDBC.DriverName");
		 String URL = bundle.getString("JDBC.URL");
		 String User = bundle.getString("JDBC.User");
		 String PassWord = bundle.getString("JDBC.Password");
		 
		 //加载数据库驱动程序
		 Class.forName(DriverName);
		 
		 return DriverManager.getConnection(URL, User, PassWord);
	}
	
	/**
	 * 关闭数据库查询结果集
	 * @param rs
	 */
	public static void close(ResultSet rs){
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				rs = null;
			}
		}
	}
	
	/**
	 * 关闭数据库的声明(操作)对象
	 * @param pstat
	 */
	public static void close( Statement pstat ) {
		try {
			if ( pstat != null ) {
				pstat.close();
			}
		} catch (SQLException e) {
			pstat = null;
		}
	}
	
	/**
	 * 关闭数据库连接对象
	 * @param conn
	 */
	public static void close( Connection conn ) {
		try {
			if ( conn != null ) {
				conn.close();
			}
		} catch (SQLException e) {
			conn = null;
		}
	}
	
	/**
	 * 启动事务
	 * @param conn
	 */
	public static void startTransaction( Connection conn ) {
		try {
			conn.setAutoCommit(false);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 提交事务
	 * @param conn
	 */
	public static void commit( Connection conn ) {
		try {
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 回滚事务
	 * @param conn
	 */
	public static void rollback( Connection conn ) {
		try {
			conn.rollback();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 结束事务
	 * @param conn
	 */
	public static void endTransaction( Connection conn ) {
		try {
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}

判断是否为邮箱

        /**
	 * 判断是否为邮箱
	 * 是:true
	 * 否:false
	 * */
	public static boolean isEmail(String mobile) {
		if (isEmpty(mobile)){
			return false;
		}else {
			String regex = "^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\\.([a-zA-Z0-9_-])+)+$";
			Pattern pattern = Pattern.compile(regex);
			Matcher m = pattern.matcher(mobile);
			return m.matches();
		}
	}

判断是否为手机号

	/**
	 * 判断是否为手机号
	 * 是:true
	 * 否:false
	 * */
	public static boolean isMobile(String mobile) {
		if (isEmpty(mobile)){
			return false;
		}else {
			String regex = "^(13|15|18)[0-9]{9}$";
			Pattern pattern = Pattern.compile(regex);
			Matcher m = pattern.matcher(mobile);
			return m.matches();
		}
	}

全角字符转换半角

/**
	 * 全角字符转换半角
	 * @param QJstr
	 * @return
	 */
	public static final String full2HalfChange(String QJstr) {
		StringBuffer outStrBuf = new StringBuffer("");
		String Tstr = "";
		byte[] b = null;
		for (int i = 0; i < QJstr.length(); i++) {
			Tstr = QJstr.substring(i, i + 1);
			// 全角空格转换成半角空格
			if (Tstr.equals(" ")) {
				outStrBuf.append(" ");
				continue;
			}
			try {
				b = Tstr.getBytes("unicode");
				// 得到 unicode 字节数据
				if (b[2] == -1) {
					// 表示全角
					b[3] = (byte) (b[3] + 32);
					b[2] = 0;
					outStrBuf.append(new String(b, "unicode"));
				} else {
					outStrBuf.append(Tstr);
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		} // end for.
		return outStrBuf.toString();

	}

下载远程服务器图片文件

/**
	 * 下载图片
	 * @param codename	远程图片名称
	 * @param codeurl	远程图片url
	 * @param request
	 * @param response
	 */
	@RequestMapping("downloadUrlJpg")
	public static void downloadUrlJpg(String codename, String codeurl,
			HttpServletRequest request, HttpServletResponse response) {

		response.setContentType("application/x-download");
		if (codeurl != null) {
			String filename = codename + ".jpg";
			try {
				URL	url = new URL(codeurl);
				HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
				httpUrl.connect();
				filename = URLEncoder.encode(filename, "UTF-8");
				response.addHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename, "utf-8"));
				InputStream inStream = httpUrl.getInputStream();
			//	BufferedInputStream	bis = new BufferedInputStream(inStream);
				OutputStream fos = response.getOutputStream();
				BufferedOutputStream oStream = new BufferedOutputStream(fos);
				byte[] b = new byte[1024];
				int i = 0;
				while ((i = inStream.read(b)) > 0) {
					oStream.write(b, 0, i);
				}
				oStream.flush();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值