`
sam406
  • 浏览: 58084 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

常用的java Utils总结

    博客分类:
  • java
阅读更多
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);
}
}  
分享到:
评论

相关推荐

    Bootstrap 模板.md

    一些常用的 Bootstrap 模板示例,你可以根据自己的需求选择合适的模板,并进行定制以满足项目需求。Bootstrap 提供了丰富的组件和样式,可以帮助你快速搭建漂亮的网站和 Web 应用程序。 markdown文本,请使用vscode等代码编辑器查看!!!

    工地试验室人员统计表.docx

    工地试验室人员统计表.docx

    安卓音乐播放器应用及其源代码+使用说明(毕设参考)

    安卓音乐播放器应用及其源代码 概述 安卓音乐播放器应用是一款全能型音乐播放器,允许你在安卓设备上听自己的所有歌曲,并且可以免费流播。需要明确的是,这些免费歌曲绝不是非法的。它们是你可以在任何地方免费聆听的歌曲。 安卓音乐播放器让用户可以从自己的音乐库中选择想要播放的歌曲,然后在手机上播放。当你离开用户界面时,音乐不会停止。在你能做到这一点之前,你的电脑上需要安装一些东西。这样当你启动应用时,它会从你的设备中选择歌曲并播放。 音乐播放器让你可以快速轻松地管理和移动所有音乐文件。这个播放器可以播放大多数类型的mp3、midi、wav、flac raw和aac文件。它还可以播放其他类型的音频文件。音乐可以按照类型、专辑、艺术家、歌曲和文件夹进行分类,以便你可以快速找到想要的内容。 安卓音乐播放器:项目详情与技术 项目标题:安卓音乐播放器源代码 摘要:安卓音乐播放器应用让你以多种方式管理和播放你的数字音乐。 项目类型:移动应用 技术:Android Studio 数据库:SQLite 项目输出 安卓音乐播放器应用输出 如何运行安卓音乐播放器应用及其源代码

    《导师训练营》互联网项目的天花板,小白月入2w.txt

    《导师训练营》互联网项目的天花板,小白月入2w

    ASP基于WEB网上聊天室设计(源代码+论文)【ASP】.zip

    ASP基于WEB网上聊天室设计(源代码+论文)【ASP】

    ASP.net 销售管理系统项目源代码+使用说明

    ASP.net 中的销售管理系统项目是什么? ASP.net 中的销售管理系统项目,通常称为销售管理软件或销售 CRM,是一种旨在简化销售流程的软件应用程序。 销售人员可以利用该系统来维护联系、跟踪交易并通过委派管理工作来节省办公桌时间。 这是基本模型,但当今的销售管理系统要复杂得多,结合了广泛的数据,例如社交资料、在线活动、与网络中其他成员的关系以及其他特征。 当人工智能等新技术应用于这些数据时,销售人员可以获得适合其特定业务的预测和建议,从而建立更智能、切合主题的客户关系。 ASP.net 中销售管理系统的优点 通过减少管理活动和集中客户信息,销售管理系统可以提高团队效率。 该系统使销售经理能够将职责分配给适当的销售人员,优先考虑良好的销售线索,并改进预测和分析。 该系统使用 ASP.net的主要优点: 更精细的优先级划分——有效的销售 CRM 可帮助经理确定客户操作的优先级并将其分配给适当的销售代表。例如,根据对客户需求的分析,系统可以帮助员工定制针对客户的促销或捆绑服务。它还会建议和过滤潜在客户,以帮助您专注于最有可能为每种产品带来收入

    汽车起重机动力系统保养.doc

    汽车起重机动力系统保养.doc

    Java 员工管理系统项目源代码(可做毕设项目参考)

    Java 员工管理系统项目是一个基于 Java 编程语言开发的桌面应用程序,旨在管理员工的信息、津贴、扣除和薪资等功能。该系统通过提供结构和工具集,使公司能够有效地管理其员工数据和薪资流程。 系统特点 员工管理:管理员可以添加、查看和更新员工信息。 津贴管理:管理员可以添加和管理员工的津贴信息。 扣除管理:管理员可以添加和管理员工的扣除信息。 搜索功能:可以通过员工 ID 搜索员工详细信息。 更新薪资:管理员可以更新员工的薪资信息。 支付管理:处理员工的支付和生成支付记录。 模块介绍 员工管理模块:管理员可以添加、查看和更新员工信息,包括员工 ID、名字、姓氏、年龄、职位和薪资等。 津贴管理模块:管理员可以添加和管理员工的津贴信息,如医疗津贴、奖金和其他津贴。 扣除管理模块:管理员可以添加和管理员工的扣除信息,如税收和其他扣除。 搜索功能模块:可以通过员工 ID 搜索员工详细信息。 更新薪资模块:管理员可以更新员工的薪资信息。 支付管理模块:处理员工的支付和生成支付记录 可以作为毕业设计项目参考

    基于图像的机器人视觉伺服系统研究

    基于图像的机器人视觉伺服系统研究

    SLAM十四讲 Windows版本编译 的ceres、g2o以及使用方法

    slam 的ceres

    # 脉搏报警 1602(1).zip

    # 脉搏报警 1602(1).zip

    ASP某企业网络公寓管理系统的设计与实现(源代码+论文)【ASP】.zip

    ASP某企业网络公寓管理系统的设计与实现(源代码+论文)【ASP】

    bootstrap模板教程.docx

    bootstrap模板

    单机成本核算统计表.docx

    单机成本核算统计表.docx

    his_equal.v

    his_equal.v

    0基本系统的学习-短视频剪辑,剪辑软件-整套33节-无水印教程,全覆盖-视频剪辑作用.txt

    0基本系统的学习-短视频剪辑,剪辑软件-整套33节-无水印教程,全覆盖-视频剪辑作用

    360数字安全:2024年3月勒索软件流行态势分析报告

    勒索软件传播至今,360 反勒索服务已累计接收到数万勒索软件感染求助。随着新型勒索软件的快速蔓延,企业数据泄露风险不断上升,勒索金额在数百万到近亿美元的勒索案件不断出现。勒索软件给企业和个人带来的影响范围越来越广,危害性也越来越大。360全网安全大脑针对勒索软件进行了全方位的监测与防御,为需要帮助的用户提供 360 反勒索服务。 2024年3月,全球新增的活跃勒索软件家族有RAWorld、RedRansomware、Kill Security等,均为双重勒索病毒。 本月针对国内主流云服务器进行的勒索攻击比例大幅提高,从大量的云服务器用户反馈的案例看,相关系统均未安装 360终端安全产品进行勒索防护,被攻击的直接原因主要是 Web 服务漏洞、数据库弱口令登录、远程桌面弱口令登录。

    风险隐患排查治理清单.xls

    风险隐患排查治理清单.xls

    职工合理化建议技术革新项目申报表.doc

    职工合理化建议技术革新项目申报表.doc

    ASP基于BS的家教交流平台的实现(源代码+论文)【ASP】.zip

    ASP基于BS的家教交流平台的实现(源代码+论文)【ASP】

Global site tag (gtag.js) - Google Analytics