• java常用方法集合


    1.获取当前日期

    // 获取当前日期
        public Date getDate(int num) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(new Date());
            cal.add(Calendar.DAY_OF_MONTH, num);
            return cal.getTime();
        }
    

    2.转换特殊字符,将json串转换为JS能直接识别的json

    /**
         * 转换特殊字符,将json串转换为JS能直接识别的json
         *
         * @param oldJson
         * @return
         * @see [相关类/方法](可选)
         * @since [产品 /模块版本](可选)
         */
        public static String getJsonForJS(String oldJson) {
            String newJson = oldJson;
            newJson = newJson.replaceAll("\\", "\\\\");
            newJson = newJson.replaceAll("\'", "\\'");
            newJson = newJson.replaceAll("\"", "\\"");
            return newJson;
        }

     3.根据图片url获取图片的base64编码

    public static String getBase64ByUrl(String urlPath){
            ByteArrayOutputStream data = new ByteArrayOutputStream();
            try {
                URL url = new URL(urlPath);
                byte[] by = new byte[1024];
                URLConnection urlConnection = url.openConnection();
                HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
                httpURLConnection.setConnectTimeout(1000*5);
                httpURLConnection.connect();
                InputStream inputStream = httpURLConnection.getInputStream();
                int len = -1;
                while ( (len = inputStream.read(by)) !=-1){
                    data.write(by,0,len);
                }
                inputStream.close();
            } catch (Exception e) {
                logger.error("获取图片base64出错:" + e + "图片url为:" + urlPath);
            }
            return Base64.getMimeEncoder().encodeToString(data.toByteArray());
        }
  • 相关阅读:
    bzoj 1821: [JSOI2010]Group 部落划分 Group
    codevs 1217 借教室
    洛谷 P2678 跳石头
    洛谷 P1316 丢瓶盖
    洛谷 P2683 小岛
    洛谷 P2431 正妹吃月饼
    loj #6092. 「Codeforces Round #418」恋爱循环
    loj #6091. 「Codeforces Round #418」幻想特快
    loj #6090. 「Codeforces Round #418」尘封思绪
    前端移植说明
  • 原文地址:https://www.cnblogs.com/jxxblogs/p/11572980.html
Copyright © 2020-2023  润新知