您的位置:首页 > 编程语言 > Java开发

常用的java Utils总结

2014-04-19 17:24 399 查看


常用的java Utils总结

HibernateUtils

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {

try {

// Create the SessionFactory from hibernate.cfg.xml

sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (Throwable ex) {

// Make sure you log the exception, as it might be swallowed

System.err.println("Initial SessionFactory creation failed." + ex);

throw new ExceptionInInitializerError(ex);

}

}

public static SessionFactory getSessionFactory() {

return sessionFactory;

}

}

DateUtils

public class DateUtils

{

//日期加(天数)

public static java.util.Date addTimeByDay(java.util.Date date,int days) throws Exception

{

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+days);

return calendar.getTime();

}

public static java.util.Date addTimeByMinutes(java.util.Date date,int minutes) throws Exception

{

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.set(Calendar.MINUTE,calendar.get(Calendar.MINUTE)+minutes);

return calendar.getTime();

}

public static java.util.Date addTimeBySeconds(java.util.Date date,int seconds) throws Exception

{

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)+seconds);

return calendar.getTime();

}

//得到当前日期

public static java.util.Date nowTime() throws Exception

{

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String datestr = sdf.format(java.util.Calendar.getInstance().getTime());

return sdf.parse(datestr);

}

//得到当前时间

public static java.util.Date nowFullTime() throws Exception

{

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String datestr = sdf.format(java.util.Calendar.getInstance().getTime());

return sdf.parse(datestr);

}

public static java.util.Date nowFullTime(String format) throws Exception

{

SimpleDateFormat sdf = new SimpleDateFormat(format);

String datestr = sdf.format(java.util.Calendar.getInstance().getTime());

return sdf.parse(datestr);

}

public static String convertDateStrToString(String datestr,String format) throws Exception

{

String result = null;

SimpleDateFormat sdf = new SimpleDateFormat(format);

try

{

result = sdf.format(sdf.parse(datestr));

}

catch (Exception ex)

{

sdf = new SimpleDateFormat("yyyy-MM-dd");

result = sdf.format(sdf.parse(datestr));

}

return result;

}

public static String convertDateToString(java.util.Date date,String format) throws Exception

{

SimpleDateFormat sdf = new SimpleDateFormat(format);

return sdf.format(date);

}

public static java.util.Date formatDateStr(String datestr) throws Exception

{

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

return sdf.parse(datestr);

}

public static java.util.Date formatDateStr(String datestr,String format) throws Exception

{

java.util.Date result = null;

SimpleDateFormat sdf = new SimpleDateFormat(format);

try

{

result = sdf.parse(datestr);

}

catch (Exception ex)

{

sdf = new SimpleDateFormat("yyyy-MM-dd");

result = sdf.parse(datestr);

}

return result;

}

public static java.util.Date formatFullDateStr(String datestr) throws Exception

{

java.util.Date result = null;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try

{

result = sdf.parse(datestr);

}

catch (Exception ex)

{

sdf = new SimpleDateFormat("yyyy-MM-dd");

result = sdf.parse(datestr);

}

return result;

}

}

NumberUtils

public class NumberUtils

{

private NumberUtils() {

}

/**

* 精确的加法运算.

*/

public static double add(double v1, double v2) {

BigDecimal b1 = new BigDecimal(v1);

BigDecimal b2 = new BigDecimal(v2);

return b1.add(b2).doubleValue();

}

/**

*

* 精确的减法运算.

*

* @param v1 被减数

* @param v2 减数

*/

public static double subtract(double v1, double v2) {

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.subtract(b2).doubleValue();

}

/**

* 提供精确的乘法运算.

*/

public static double multiply(double v1, double v2) {

BigDecimal b1 = new BigDecimal(v1);

BigDecimal b2 = new BigDecimal(v2);

return b1.multiply(b2).doubleValue();

}

/**

* 提供精确的乘法运算,并对运算结果截位.

*

* @param scale 运算结果小数后精确的位数

*/

public static double multiply(double v1, double v2,int scale) {

if (scale < 0) {

throw new IllegalArgumentException("The scale must be a positive integer or zero");

}

BigDecimal b1 = new BigDecimal(v1);

BigDecimal b2 = new BigDecimal(v2);

return b1.multiply(b2).setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();

}

/**

* 提供(相对)精确的除法运算.

*

* @see #divide(double, double, int)

*/

public static double divide(double v1, double v2) {

BigDecimal b1 = new BigDecimal(v1);

BigDecimal b2 = new BigDecimal(v2);

return b1.divide(b2).doubleValue();

}

/**

* 提供(相对)精确的除法运算.

* 由scale参数指定精度,以后的数字四舍五入.

*

* @param v1 被除数

* @param v2 除数

* @param scale 表示表示需要精确到小数点以后几位

*/

public static double divide(double v1, double v2, int scale) {

if (scale < 0) {

throw new IllegalArgumentException("The scale must be a positive integer or zero");

}

BigDecimal b1 = new BigDecimal(v1);

BigDecimal b2 = new BigDecimal(v2);

return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();

}

/**

* 提供精确的小数位四舍五入处理.

*

* @param v 需要四舍五入的数字

* @param scale 小数点后保留几位

*/

public static double round(double v, int scale) {

if (scale < 0) {

throw new IllegalArgumentException("The scale must be a positive integer or zero");

}

BigDecimal b = new BigDecimal(v);

return b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();

}

}

JdbcUtils

public class JdbcUtils {

static{

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (Exception e) {

e.printStackTrace();

}

}

public static Connection getConnection(){

Connection con = null;

try {

con= DriverManager.getConnection(url,name,password));

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return con;

}

public static void close(ResultSet rs,PreparedStatement ps , Connection con) {

try {

if(rs!=null)

rs.close();

if(ps!=null)

ps.close();

if(con!=null)

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

EncodeUtils

public class EncodeUtils {

private static final String DEFAULT_URL_ENCODING = "UTF-8";

/**

* Hex编码.

*/

public static String hexEncode(byte[] input) {

return Hex.encodeHexString(input);

}

/**

* Hex解码.

*/

public static byte[] hexDecode(String input) {

try {

return Hex.decodeHex(input.toCharArray());

} catch (DecoderException e) {

throw new IllegalStateException("Hex Decoder exception", e);

}

}

/**

* Base64编码.

*/

public static String base64Encode(byte[] input) {

return new String(Base64.encodeBase64(input));

}

/**

* Base64编码, URL安全(将Base64中的URL非法字符如+,/=转为其他字符, 见RFC3548).

*/

public static String base64UrlSafeEncode(byte[] input) {

return Base64.encodeBase64URLSafeString(input);

}

/**

* Base64解码.

*/

public static byte[] base64Decode(String input) {

return Base64.decodeBase64(input);

}

/**

* URL 编码, Encode默认为UTF-8.

*/

public static String urlEncode(String input) {

return urlEncode(input, DEFAULT_URL_ENCODING);

}

/**

* URL 编码.

*/

public static String urlEncode(String input, String encoding) {

try {

return URLEncoder.encode(input, encoding);

} catch (UnsupportedEncodingException e) {

throw new IllegalArgumentException("Unsupported Encoding Exception", e);

}

}

/**

* URL 解码, Encode默认为UTF-8.

*/

public static String urlDecode(String input) {

return urlDecode(input, DEFAULT_URL_ENCODING);

}

/**

* URL 解码.

*/

public static String urlDecode(String input, String encoding) {

try {

return URLDecoder.decode(input, encoding);

} catch (UnsupportedEncodingException e) {

throw new IllegalArgumentException("Unsupported Encoding Exception", e);

}

}

/**

* Html 转码.

*/

public static String htmlEscape(String html) {

return StringEscapeUtils.escapeHtml(html);

}

/**

* Html 解码.

*/

public static String htmlUnescape(String htmlEscaped) {

return StringEscapeUtils.unescapeHtml(htmlEscaped);

}

/**

* Xml 转码.

*/

public static String xmlEscape(String xml) {

return StringEscapeUtils.escapeXml(xml);

}

/**

* Xml 解码.

*/

public static String xmlUnescape(String xmlEscaped) {

return StringEscapeUtils.unescapeXml(xmlEscaped);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: