zoukankan      html  css  js  c++  java
  • web开发工具类

    1.日期工具类

    import java.text.SimpleDateFormat;

    import java.util.Date;

    public class DateUtil {

    public static String formatDate(Date date,String format){
    String result="";
    SimpleDateFormat sdf=new SimpleDateFormat(format);
    if(date!=null){
    result=sdf.format(date);
    }
    return result;
    }


    public static Date formatString(String str,String format) throws Exception{
    SimpleDateFormat sdf=new SimpleDateFormat(format);
    return sdf.parse(str);
    }
    }

    2.结果集转JSONArray工具类

    import java.sql.ResultSet;

    import java.sql.ResultSetMetaData;
    import java.util.Date;

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;

    public class JsonUtil {

    public static JSONArray formatRsToJsonArray(ResultSet rs)throws Exception{
    ResultSetMetaData md=rs.getMetaData();
    int num=md.getColumnCount();
    JSONArray array=new JSONArray();
    while(rs.next()){
    JSONObject mapOfColValues=new JSONObject();
    for(int i=1;i<=num;i++){
    Object o=rs.getObject(i);
    if(o instanceof Date){
    mapOfColValues.put(md.getColumnName(i), DateUtil.formatDate((Date)o, "yyyy-MM-dd"));
    }else{
    mapOfColValues.put(md.getColumnName(i), rs.getObject(i));
    }
    }
    array.add(mapOfColValues);
    }
    return array;
    }
    }

    3.输出json字符串工具类

    import java.io.PrintWriter;

    import javax.servlet.http.HttpServletResponse;

    import net.sf.json.JSONObject;

    public class ResponseUtil {

    public static void write(HttpServletResponse response,Object o)throws Exception{
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out=response.getWriter();
    out.println(o.toString());
    out.flush();
    out.close();
    }
    }

    4.判断字符串空或非空工具类

    public class StringUtil {

    public static boolean isEmpty(String str){
    if("".equals(str)|| str==null){
    return true;
    }else{
    return false;
    }
    }

    public static boolean isNotEmpty(String str){
    if(!"".equals(str)&&str!=null){
    return true;
    }else{
    return false;
    }
    }
    }

  • 相关阅读:
    Android 使用Application总结
    android数据保存
    Android 利用Application对象存取公共数据
    android 通过post方式提交数据的最简便有效的方法
    android http协议post请求方式
    maven下载及配置
    普通的101键盘在Mac上的键位对应
    高效使用你的Xcode
    maven安装及maven项目导入流程
    Mac键盘图标与对应快捷按键标志汇总
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/5146520.html
Copyright © 2011-2022 走看看