关闭

java 常用类

标签: java
498人阅读 评论(0) 收藏 举报
分类:

1. ConDataSource

package javacommon.util;

import java.sql.ResultSet;
import java.sql.Statement;

import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.support.rowset.SqlRowSet;


public class ConDataSource {

	//config.properties 获取配置
// public static DriverManagerDataSource getDataSource() throws Exception {
//		DriverManagerDataSource ds = new DriverManagerDataSource();
//		ds.setDriverClassName(Global.getJdbcDriver());
//		ds.setUrl(Global.getJdbcUrl());
//		ds.setUsername(Global.getJdbcUser());
//		ds.setPassword(Global.getJdbcPassword());
//		return ds;
//	}
	
//从Spring applicationContext 获取配置
//获取spring配置文件函数由FileSystemXmlApplicationContext换成ClassPathXmlApplicationContext,只需要文件名,不需要全路径  
	public static DataSource getDataSourceFromXML() throws Exception {
		String acFile="spring/applicationContext-datasource.xml";
		String dsStr="dataSource";
		ApplicationContext ac = new ClassPathXmlApplicationContext(acFile);
		//ApplicationContext ac = new FileSystemXmlApplicationContext(acFile);
		DataSource dataSource = (DataSource) ac.getBean(dsStr);
		//System.out.println("连接成功!");
		return dataSource;
	}
	
	
	
///////////////////////////////////	
	public static SqlRowSet getAllResult(DataSource dataSource,String querySql) throws  Exception {
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		SqlRowSet rs = jt.queryForRowSet(querySql);
		return rs;
	}

	public static int execSql(DataSource dataSource,String updateSql) throws  Exception {
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		int count =jt.update(updateSql);		
		return count;
	}	
	
	
	public static SqlRowSet getAllResult(String querySql) throws  Exception {
		DataSource dataSource = ConDataSource.getDataSourceFromXML();
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		SqlRowSet rs = jt.queryForRowSet(querySql);
		return rs;
	}
	public static String getForStr (String querySql,String field) throws  Exception {
		DataSource dataSource = ConDataSource.getDataSourceFromXML();
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		SqlRowSet rs = jt.queryForRowSet(querySql);
		
		String str=rs.next()?rs.getString(field):null;
		return str;
	}
	public static int execSql(String updateSql) throws  Exception {
		DataSource dataSource = ConDataSource.getDataSourceFromXML();
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		int count =jt.update(updateSql);		
		return count;
	}
	
	public static String getForStr (DataSource dataSource,String querySql,String field) throws  Exception { 
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		SqlRowSet rs = jt.queryForRowSet(querySql);
		
		String str=rs.next()?rs.getString(field):null;
		return str;
	}
	 
}

2 DateTimeUtil

 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.log4j.Logger;

public final class DateTimeUtil {

	private static Logger logger = Logger.getLogger(DateTimeUtil.class);


	/**
	 * 解析:当前日期
	 * 
	 * @return String
	 */
	public final static String parseNowAsString() {
		String strResult = "";
		String strFormat = "yyyyMMdd";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
		Calendar calendar = Calendar.getInstance();
		strResult = simpleDateFormat.format(calendar.getTime());
		simpleDateFormat = null;
		strFormat = null;
		calendar = null;
		return strResult;
	}
	
	public final static Date getNowDate() {
		//Calendar.getInstance().get
		return new Date();
	}
	/**
	 * 解析:当前日期
	 * 
	 * @return String
	 */
	public final static String parseNowYear() {
		String strResult = "";
		String strFormat = "yyyy";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
		Calendar calendar = Calendar.getInstance();
		strResult = simpleDateFormat.format(calendar.getTime());
		simpleDateFormat = null;
		strFormat = null;
		calendar = null;
		return strResult;
	}
	/**
	 * 解析:当前日期
	 * 
	 * @return String
	 */
	public final static String parseNowData() {
		String strResult = "";
		String strFormat = "yyyy年MM月dd日";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
		Calendar calendar = Calendar.getInstance();
		strResult = simpleDateFormat.format(calendar.getTime());
		simpleDateFormat = null;
		strFormat = null;
		calendar = null;
		return strResult;
	}
	/**
	 * 解析:指定Date
	 * 
	 * @return String
	 */
	public final static String parseDateAsString(Date date) {
		String strResult = "";
		strResult = parseDateAsString(date, null);
		date = null;
		return strResult;
	}
	/**
	 * 解析:指定Date   年-月-日
	 * @param date
	 * @return
	 */
	public final static String parseDateAsStringByNyr(Date date) {
		String strResult = "";
		strResult = parseDateAsStringBynyr(date, null);
		date = null;
		return strResult;
	}
	/**
	 * 解析:从指定Millisecond
	 * 
	 * @return String
	 */
	public final static String parseMilliAsString(long longMillisecond) {
		String strResult = "";
		Date date = new Date(longMillisecond);
		strResult = parseDateAsString(date, null);
		date = null;
		return strResult;
	}

	/**
	 * 解析:指定日期Date的毫秒
	 * 
	 * @param date
	 *            java.util.Date
	 * @return long
	 * @exception 发生异常时,返回0
	 */
	public final static long parseDateAsLong(Date date) {
		long longResult = 0;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		longResult = calendar.getTimeInMillis();
		// 释放对象
		date = null;
		calendar = null;
		return longResult;
	}

	/**
	 * 解析:指定日期字符串
	 * 
	 * @param date
	 *            java.util.Date
	 * @return long
	 * @exception 发生异常时,返回0
	 */
	public final static long parseStringAsLong(String strDate) {
		long longResult = 0;
		String strFormat = "yyyy-MM-dd HH:mm:ss";
		DateFormat dateFormat = new SimpleDateFormat(strFormat);
		Date date = null;
		try {
			date = dateFormat.parse(strDate);
			longResult = parseDateAsLong(date);
		} catch (ParseException e) {
			logger.error(e);
		}
		// 释放对象
		date = null;
		dateFormat = null;
		strFormat = null;
		return longResult;
	}

	/**
	 * 解析:指定日期字符串
	 * 
	 * @param date
	 *            java.util.Date
	 * @return long
	 * @exception 发生异常时,返回0
	 */
	public final static Date parseStringAsDate(String strDate) {
		Date date = null;
		String strFormat = "yyyy-MM-dd HH:mm:ss";
		DateFormat dateFormat = new SimpleDateFormat(strFormat);
		try {
			date = dateFormat.parse(strDate);
		} catch (ParseException e) {
			logger.error(e);
		}
		// 释放对象
		dateFormat = null;
		strFormat = null;
		return date;
	}
	/**
	 * 解析:指定日期字符串
	 * 
	 * @param date
	 *            java.util.Date
	 * @return long
	 * @exception 发生异常时,返回0
	 */
	public final static Date parseStringAsDatea(String strDate) {
		Date date = null;
		String strFormat = "yyyy-MM-dd";
		DateFormat dateFormat = new SimpleDateFormat(strFormat);
		try {
			date = dateFormat.parse(strDate);
		} catch (ParseException e) {
			logger.error(e);
		}
		// 释放对象
		dateFormat = null;
		strFormat = null;
		return date;
	}

	/**
	 * 日期加减操作
	 * 
	 * @param date
	 * @param intType
	 *            日期类型,例如:Calendar.YEAR
	 * @param intIntervalValue
	 *            间隔值
	 * @return Date
	 */
	public final static Date dateOperation(Calendar calendar, int intType,
			int intIntervalValue) {
		Date dateResult = null;
		calendar.add(intType, intIntervalValue);
		dateResult = calendar.getTime();
		calendar = null;
		return dateResult;
	}

	/**
	 * 现在日期加减操作
	 * 
	 * @param type
	 *            日期类型,例如:Calendar.YEAR
	 * @param intervalValue
	 *            间隔值
	 * @return Date
	 */
	public final static Date nowOperation(int intType, int intIntervalValue) {
		Calendar calendar = Calendar.getInstance();
		return dateOperation(calendar, intType, intIntervalValue);
	}

	/**
	 * 根据指定的日期格式格式化日期
	 * 
	 * @param date
	 * @param strFormat
	 * @return String
	 */
	public final static String parseDateAsString(Date date, String strFormat) {
		String strResult = "";
		if (strFormat == null || strFormat.trim().length() <= 0) {
			strFormat = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
		strResult = simpleDateFormat.format(date);
		return strResult;
	}
	/**
	 * 根据指定的日期格式格式化日期 yyyy-MM-dd
	 * @param date
	 * @param strFormat
	 * @return
	 */
	public final static String parseDateAsStringBynyr(Date date, String strFormat) {
		String strResult = "";
		if (strFormat == null || strFormat.trim().length() <= 0) {
			strFormat = "yyyy-MM-dd";
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
		strResult = simpleDateFormat.format(date);
		return strResult;
	}
	
	/**
	 * 解析:当前日期转化为yyyyMMddHHmmss
	 * 
	 * @return String
	 */
	public final static String parseNowAsStringFoamt(Calendar calendar) {
		String strResult = "";
		String strFormat = "yyyyMMddHHmmss";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormat);
		//Calendar calendar = Calendar.getInstance();
		strResult = simpleDateFormat.format(calendar.getTime());
		simpleDateFormat = null;
		strFormat = null;
		calendar = null;
		return strResult;
	}
	
	public static void main(String args[]) {
		//Calendar calendar = Calendar.getInstance();
		System.out.println(parseNowAsStringFoamt(Calendar.getInstance()));
		System.out.println(parseDateAsString(new Date()));
		System.out.println(parseNowYear());
		
		
	}

}

.。。。。。


http://download.csdn.net/detail/maxuyang1987/6656419

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:111629次
    • 积分:1410
    • 等级:
    • 排名:千里之外
    • 原创:39篇
    • 转载:20篇
    • 译文:0篇
    • 评论:19条
    文章分类
    最新评论