Java 自定义方法 - BASE:修订间差异
跳到导航
跳到搜索
(创建页面,内容为“Java 自定义函数 - BASE {| class="wikitable" |+ !No !Method !Explain !Example |- |1 |logger |日志 | |- |2 |dt |日期、时间 | |- |3 |hash, md5 |sha, sha-256, md5 等 hash 函数 | |- |4 |base64, ubase64 |BASE64 | |- |5 |des, udes |DES 加密 | |- |6 |str2map |String to MAP | |- |7 |list2map |List to MAP | |- |8 |reverse |反转字符串 | |- |9 |trim, rtrim, trim |remove substring | |} package com.udf.base; /* ---------------------------------…”) |
无编辑摘要 |
||
第8行: | 第8行: | ||
|- | |- | ||
|1 | |1 | ||
| | |isnull | ||
|空(null, "", [], {}) = true | |||
| | |||
|- | |||
|2 | |||
|log, logerror | |||
|日志 | |日志 | ||
| | | | ||
|- | |- | ||
| | |3 | ||
|dt | |dt | ||
|日期、时间 | |日期、时间 | ||
| | | | ||
|- | |- | ||
| | |4 | ||
|hash, md5 | |hash, md5 | ||
|sha, sha-256, md5 等 hash 函数 | |sha, sha-256, md5 等 hash 函数 | ||
| | | | ||
|- | |- | ||
| | |5 | ||
|base64, ubase64 | |base64, ubase64 | ||
|BASE64 | |BASE64 | ||
| | | | ||
|- | |- | ||
| | |6 | ||
|des, udes | |des, udes | ||
|DES 加密 | |DES 加密 | ||
| | | | ||
|- | |- | ||
| | |7 | ||
|str2map | |str2map | ||
|String to MAP | |String to MAP | ||
| | | | ||
|- | |- | ||
| | |8 | ||
|list2map | |list2map | ||
|List to MAP | |List to MAP | ||
| | | | ||
|- | |- | ||
| | |9 | ||
|reverse | |reverse | ||
|反转字符串 | |反转字符串 | ||
| | | | ||
|- | |- | ||
| | |10 | ||
|trim, rtrim, trim | |trim, rtrim, trim | ||
|remove substring | |remove substring | ||
| | | | ||
|} | |} | ||
package com.udf.base; | <small><small>package com.udf.base; | ||
/* | /* | ||
------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ||
第63行: | 第68行: | ||
1.0 2024/2/20 Adam | 1.0 2024/2/20 Adam | ||
1.1 2024/2/27 Adam merge STR | 1.1 2024/2/27 Adam merge STR | ||
1.2 2024/2/29 Adam remove commons-codec, replace util. | 1.2 2024/2/29 Adam remove commons-codec, replace util | ||
1.21 2024/3/20 Adam Add log, reverse2 | |||
1.22 2024/3/28 Adam Add isnull, log4j --> slf4j | |||
format: | format: | ||
object : (string) | object : (string) | ||
property: | property: | ||
method : dt, hash, md5, base64, des, ltrim, rtrim, trim, reverse | method : isnull, log, dt, hash, md5, base64, des, ltrim, rtrim, trim, reverse | ||
<nowiki><!--log4j--></nowiki> | <nowiki><!-- Json --></nowiki> | ||
<dependency> | |||
<groupId>com.google.code.gson</groupId> | |||
<artifactId>gson</artifactId> | |||
<version>2.8.9</version> | |||
</dependency> | |||
<nowiki><!-- slf4j - log4j --></nowiki> | |||
<dependency> | <dependency> | ||
<groupId>log4j</groupId> | <groupId>log4j</groupId> | ||
第76行: | 第89行: | ||
<version>1.2.17</version> | <version>1.2.17</version> | ||
</dependency> | </dependency> | ||
<dependency> | |||
<groupId>org.slf4j</groupId> | |||
<artifactId>slf4j-api</artifactId> | |||
<version>2.0.9</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.slf4j</groupId> | |||
<artifactId>slf4j-log4j12</artifactId> | |||
<version>2.0.9</version> | |||
</dependency> | |||
------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ||
*/ | */ | ||
import | import com.google.gson.Gson; | ||
import org.apache.log4j.PropertyConfigurator; | import org.apache.log4j.PropertyConfigurator; | ||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import javax.crypto.Cipher; | import javax.crypto.Cipher; | ||
第92行: | 第116行: | ||
import java.security.SecureRandom; | import java.security.SecureRandom; | ||
import java.text.SimpleDateFormat; | import java.text.SimpleDateFormat; | ||
import java.util. | import java.util.*; | ||
public class BASE { | public class BASE { | ||
public static final String VERSION = "v1. | public static final String VERSION = "v1.22"; | ||
static { | static { | ||
PropertyConfigurator.configure("config/log4j.properties"); | PropertyConfigurator.configure("config/log4j.properties"); | ||
} | } | ||
public static Gson json = new Gson(); | |||
// get sysdate, "-:" --> 2024-02-22 00:00:00 | public static boolean UDEFLOGOFF = false; | ||
public static boolean isnull(Object obj1, boolean default1) { | |||
if (obj1 == null) return true; | |||
if (obj1 instanceof String) { return ((String) obj1).isEmpty(); } | |||
else if (obj1 instanceof List) { return ((List) obj1).isEmpty(); } | |||
else if (obj1 instanceof Map) { return ((Map) obj1).isEmpty(); } | |||
else if (obj1 instanceof Properties) { return ((Properties) obj1).isEmpty(); } | |||
else { return default1; } | |||
// Default behavior for unknown types (e.g., return false) | |||
} | |||
public static boolean isnull(Object obj1) { | |||
return isnull(obj1, false); | |||
} | |||
public static void log(Object log1) { | |||
if (UDEFLOGOFF) return; | |||
Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); | |||
if (log1==null) | |||
logger.info(""); | |||
else | |||
logger.info(log1.toString()); | |||
} | |||
public static void logwarn(Object log1) { | |||
if (UDEFLOGOFF) return; | |||
Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); | |||
if (log1==null) | |||
logger.warn(""); | |||
else | |||
logger.warn(log1.toString()); | |||
} | |||
public static void logerror(Object log1) { | |||
if (UDEFLOGOFF) return; | |||
Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); | |||
if (log1==null) | |||
logger.error(""); | |||
else | |||
logger.error(log1.toString()); | |||
} | |||
// get sysdate, "-:" --> 2024-02-22 00:00:00.000 | |||
public static String dt(String p, int len) { | public static String dt(String p, int len) { | ||
SimpleDateFormat df = new SimpleDateFormat(); | SimpleDateFormat df = new SimpleDateFormat(); | ||
if (p==null||p.length() <2) | if (p==null||p.length() <2) | ||
df.applyPattern(" | df.applyPattern("yyyyMMddHHmmssSSS"); | ||
else | else | ||
df.applyPattern("yyyy"+p.substring(0,1)+"MM"+p.substring(0,1)+"dd HH"+p.substring(1,2)+"mm"+p.substring(1,2)+"ss"); | df.applyPattern("yyyy"+p.substring(0,1)+"MM"+p.substring(0,1)+"dd HH"+p.substring(1,2)+"mm"+p.substring(1,2)+"ss"+"."+"SSS"); | ||
Date date = new Date(); | Date date = new Date(); | ||
第128行: | 第187行: | ||
public static String hash(String s1, String type) { | public static String hash(String s1, String type) { | ||
if ((s1==null)||(s1.equals(""))) { | if ((s1==null)||(s1.equals(""))) { | ||
return ""; | return ""; | ||
} | |||
if ((type==null)||(type.equals(""))) { | |||
type = "md5"; | |||
} | } | ||
String hash1 = ""; | String hash1 = ""; | ||
第137行: | 第198行: | ||
hash1 = new BigInteger(1, md.digest()).toString(16); | hash1 = new BigInteger(1, md.digest()).toString(16); | ||
} catch (NoSuchAlgorithmException e) { | } catch (NoSuchAlgorithmException e) { | ||
logerror(e); | |||
} | } | ||
return hash1; | return hash1; | ||
第143行: | 第204行: | ||
public static String md5(String s1) { | public static String md5(String s1) { | ||
return hash(s1, "md5"); | return hash(s1, "md5"); | ||
} | } | ||
public static String base64(String s1) { | public static String base64(String s1) { | ||
if ((s1==null)||(s1.equals(""))) { | |||
return ""; | |||
} | |||
try { | try { | ||
String b64s1 = Base64.getEncoder().encodeToString(s1.getBytes("utf-8")); | String b64s1 = Base64.getEncoder().encodeToString(s1.getBytes("utf-8")); | ||
return b64s1; | return b64s1; | ||
} catch (Exception e){ | } catch (Exception e){ | ||
logerror(e); | |||
return null; | return null; | ||
} | } | ||
} | } | ||
public static String ubase64(String s1) { | public static String ubase64(String s1) { | ||
if ((s1==null)||(s1.equals(""))) { | |||
return ""; | |||
} | |||
try { | try { | ||
byte[] b1 = Base64.getDecoder().decode(s1); | byte[] b1 = Base64.getDecoder().decode(s1); | ||
return new String(b1, "utf-8"); | return new String(b1, "utf-8"); | ||
} catch (Exception e){ | } catch (Exception e){ | ||
logerror(e); | |||
return null; | return null; | ||
} | } | ||
第167行: | 第231行: | ||
public static String des(String s1, String k1) { | public static String des(String s1, String k1) { | ||
if (s1 == null) { | if (s1 == null) { | ||
log("The parameter is NULL."); | |||
return null; | return null; | ||
} | } | ||
while (k1.length() < 8){ | while (k1.length() < 8){ | ||
log("The parameter is short."); | |||
k1 += "0"; | k1 += "0"; | ||
} | } | ||
第190行: | 第254行: | ||
return Base64.getEncoder().encodeToString(b1); | return Base64.getEncoder().encodeToString(b1); | ||
} catch (Exception e) { | } catch (Exception e) { | ||
logerror(e); | |||
return null; | return null; | ||
} | } | ||
第196行: | 第260行: | ||
public static String udes(String s1, String k1) { | public static String udes(String s1, String k1) { | ||
if (s1 == null) { | if (s1 == null) { | ||
log("The parameter is NULL."); | |||
return null; | return null; | ||
} | } | ||
while (k1.length() < 8) { | while (k1.length() < 8) { | ||
log("The parameter is short."); | |||
k1 += "0"; | k1 += "0"; | ||
} | } | ||
第216行: | 第280行: | ||
return new String(cipher.doFinal(Base64.getDecoder().decode(s1)), Charset); | return new String(cipher.doFinal(Base64.getDecoder().decode(s1)), Charset); | ||
} catch (Exception e) { | } catch (Exception e) { | ||
logerror(e); | |||
return null; | return null; | ||
} | } | ||
第261行: | 第325行: | ||
} | } | ||
// reverse a string, like: abc --> cba | // reverse a string, like: abc --> cba | ||
public static String reverse(String | public static String reverse(String s1) { | ||
char[] cStr1 = | if ((s1==null)||(s1.equals(""))) { | ||
return ""; | |||
} | |||
char[] cStr1 = s1.toCharArray(); | |||
int len1 = cStr1.length; | int len1 = cStr1.length; | ||
for (int i = 0; i < len1 / 2; i++) { | for (int i = 0; i < len1 / 2; i++) { | ||
第271行: | 第338行: | ||
String rStr1 = new String(cStr1); | String rStr1 = new String(cStr1); | ||
return rStr1; | return rStr1; | ||
} | |||
public static String reverse2(String s1) { | |||
if ((s1==null)||(s1.equals(""))) { | |||
return ""; | |||
} | |||
StringBuffer sf1 = new StringBuffer(128); | |||
sf1.append(s1); | |||
return sf1.reverse().toString(); | |||
} | } | ||
// left remove a substring | // left remove a substring | ||
public static String ltrim(String str1, String str2) { | public static String ltrim(String str1, String str2) { | ||
if (str1 == null) return str1; | |||
if (str1.indexOf(str2) == 0) | if (str1.indexOf(str2) == 0) | ||
return str1.substring(str2.length()); | return str1.substring(str2.length()); | ||
第283行: | 第359行: | ||
String s1, s2; | String s1, s2; | ||
if (i == 0) | if (i == 0) | ||
i = | i = 1024; | ||
s1 = str1; | s1 = str1; | ||
s2 = str1; | s2 = str1; | ||
第297行: | 第373行: | ||
// right remove a substring | // right remove a substring | ||
public static String rtrim(String str1, String str2) { | public static String rtrim(String str1, String str2) { | ||
return rtrim(str1, str2, 1); | |||
} | } | ||
// right remove i substrings | // right remove i substrings | ||
第312行: | 第382行: | ||
if (i == 0) | if (i == 0) | ||
i = | i = 1024; | ||
s1 = rstr1; | s1 = rstr1; | ||
s2 = rstr1; | s2 = rstr1; | ||
第326行: | 第396行: | ||
// left & right remove a substring | // left & right remove a substring | ||
public static String trim(String str1, String str2) { | public static String trim(String str1, String str2) { | ||
return rtrim(ltrim(str1, str2), str2); | |||
} | } | ||
// left & right remove i substring | // left & right remove i substring | ||
public static String trim(String str1, String str2, int i) { | public static String trim(String str1, String str2, int i) { | ||
return rtrim(ltrim(str1, str2, i), str2, i); | |||
} | } | ||
} | }</small></small> | ||
[[分类:Develop]] | [[分类:Develop]] | ||
[[分类:Java]] | [[分类:Java]] |
2024年3月29日 (五) 14:42的版本
Java 自定义函数 - BASE
No | Method | Explain | Example |
---|---|---|---|
1 | isnull | 空(null, "", [], {}) = true | |
2 | log, logerror | 日志 | |
3 | dt | 日期、时间 | |
4 | hash, md5 | sha, sha-256, md5 等 hash 函数 | |
5 | base64, ubase64 | BASE64 | |
6 | des, udes | DES 加密 | |
7 | str2map | String to MAP | |
8 | list2map | List to MAP | |
9 | reverse | 反转字符串 | |
10 | trim, rtrim, trim | remove substring |
package com.udf.base; /* ------------------------------------------------------------------------------ Name : Udf.base.BASE Purpose : string crypt & decrypt, datetime Author : Adam Revisions: Ver Date Author Description --------- ---------- --------------- ------------------------------------ 1.0 2024/2/20 Adam 1.1 2024/2/27 Adam merge STR 1.2 2024/2/29 Adam remove commons-codec, replace util 1.21 2024/3/20 Adam Add log, reverse2 1.22 2024/3/28 Adam Add isnull, log4j --> slf4j format: object : (string) property: method : isnull, log, dt, hash, md5, base64, des, ltrim, rtrim, trim, reverse <!-- Json --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency> <!-- slf4j - log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.9</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>2.0.9</version> </dependency> ------------------------------------------------------------------------------ */ import com.google.gson.Gson; import org.apache.log4j.PropertyConfigurator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.math.BigInteger; import java.security.Key; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.text.SimpleDateFormat; import java.util.*; public class BASE { public static final String VERSION = "v1.22"; static { PropertyConfigurator.configure("config/log4j.properties"); } public static Gson json = new Gson(); public static boolean UDEFLOGOFF = false; public static boolean isnull(Object obj1, boolean default1) { if (obj1 == null) return true; if (obj1 instanceof String) { return ((String) obj1).isEmpty(); } else if (obj1 instanceof List) { return ((List) obj1).isEmpty(); } else if (obj1 instanceof Map) { return ((Map) obj1).isEmpty(); } else if (obj1 instanceof Properties) { return ((Properties) obj1).isEmpty(); } else { return default1; } // Default behavior for unknown types (e.g., return false) } public static boolean isnull(Object obj1) { return isnull(obj1, false); } public static void log(Object log1) { if (UDEFLOGOFF) return; Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); if (log1==null) logger.info(""); else logger.info(log1.toString()); } public static void logwarn(Object log1) { if (UDEFLOGOFF) return; Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); if (log1==null) logger.warn(""); else logger.warn(log1.toString()); } public static void logerror(Object log1) { if (UDEFLOGOFF) return; Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); if (log1==null) logger.error(""); else logger.error(log1.toString()); } // get sysdate, "-:" --> 2024-02-22 00:00:00.000 public static String dt(String p, int len) { SimpleDateFormat df = new SimpleDateFormat(); if (p==null||p.length() <2) df.applyPattern("yyyyMMddHHmmssSSS"); else df.applyPattern("yyyy"+p.substring(0,1)+"MM"+p.substring(0,1)+"dd HH"+p.substring(1,2)+"mm"+p.substring(1,2)+"ss"+"."+"SSS"); Date date = new Date(); p = df.format(date); len = (len<1||len>p.length())?p.length():len; return p.substring(0, len); } public static String dt(String p) { return dt(p, 0); } public static String dt(int len) { return dt(null, len); } // get sysdate, 20240222000000 public static String dt() { return dt(14); } public static String hash(String s1, String type) { if ((s1==null)||(s1.equals(""))) { return ""; } if ((type==null)||(type.equals(""))) { type = "md5"; } String hash1 = ""; try { MessageDigest md = MessageDigest.getInstance(type); md.update(s1.getBytes()); hash1 = new BigInteger(1, md.digest()).toString(16); } catch (NoSuchAlgorithmException e) { logerror(e); } return hash1; } public static String md5(String s1) { return hash(s1, "md5"); } public static String base64(String s1) { if ((s1==null)||(s1.equals(""))) { return ""; } try { String b64s1 = Base64.getEncoder().encodeToString(s1.getBytes("utf-8")); return b64s1; } catch (Exception e){ logerror(e); return null; } } public static String ubase64(String s1) { if ((s1==null)||(s1.equals(""))) { return ""; } try { byte[] b1 = Base64.getDecoder().decode(s1); return new String(b1, "utf-8"); } catch (Exception e){ logerror(e); return null; } } public static String des(String s1, String k1) { if (s1 == null) { log("The parameter is NULL."); return null; } while (k1.length() < 8){ log("The parameter is short."); k1 += "0"; } String DES_ECB = "DES/ECB/PKCS5Padding"; String Charset = "UTF-8"; try { // get key DESKeySpec dk1 = new DESKeySpec(k1.getBytes(Charset)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); Key secretKey = keyFactory.generateSecret(dk1); Cipher cipher = Cipher.getInstance(DES_ECB); cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom()); byte[] b1 = cipher.doFinal(s1.getBytes(Charset)); // JDK1.7 及以下可以使用 BASE64Encoder return Base64.getEncoder().encodeToString(b1); } catch (Exception e) { logerror(e); return null; } } public static String udes(String s1, String k1) { if (s1 == null) { log("The parameter is NULL."); return null; } while (k1.length() < 8) { log("The parameter is short."); k1 += "0"; } try { String DES_ECB = "DES/ECB/PKCS5Padding"; String Charset = "UTF-8"; // get key DESKeySpec dk1 = new DESKeySpec(k1.getBytes(Charset)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); Key secretKey = keyFactory.generateSecret(dk1); Cipher cipher = Cipher.getInstance(DES_ECB); cipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom()); return new String(cipher.doFinal(Base64.getDecoder().decode(s1)), Charset); } catch (Exception e) { logerror(e); return null; } } public static String des(String s1) { return des(s1, "DeiPd8ltuAE3"); } public static String udes(String s1) { return udes(s1, "DeiPd8ltuAE3"); } // String(a=1 b=c...) --> map({a=1, b=c, ...}) public static HashMap<String, String> str2map (String[] s1) { HashMap<String, String> para = new HashMap<>(); String s11, key, val; String[] s2; for (int i=0; i<s1.length; i++) { try { s11 = s1[i]; s2 = s11.split("="); key = s2[0]; val = ltrim(s11, key+"="); if (null == key || "".equals(key)) { key = "" + i; } else if (key.equals(s11)) { val = key; key = "" + i; } para.put(key, val); } catch (Exception e) { para.put("" + i, s1[i].substring(1)); } } return para; } // list1, list2 --> map(list1, list2) // if list2 < list1, set to null. public static HashMap<String, String> list2map(ArrayList<String> ky1, ArrayList<String> val1) { HashMap<String, String> map1 = new HashMap<>(); for (int i=0; i<ky1.size(); i++) { map1.put(ky1.get(i), i>=val1.size()?null:val1.get(i)); } return map1; } // reverse a string, like: abc --> cba public static String reverse(String s1) { if ((s1==null)||(s1.equals(""))) { return ""; } char[] cStr1 = s1.toCharArray(); int len1 = cStr1.length; for (int i = 0; i < len1 / 2; i++) { char c1 = cStr1[i]; cStr1[i] = cStr1[len1 - 1 - i]; cStr1[len1 - 1 - i] = c1; } String rStr1 = new String(cStr1); return rStr1; } public static String reverse2(String s1) { if ((s1==null)||(s1.equals(""))) { return ""; } StringBuffer sf1 = new StringBuffer(128); sf1.append(s1); return sf1.reverse().toString(); } // left remove a substring public static String ltrim(String str1, String str2) { if (str1 == null) return str1; if (str1.indexOf(str2) == 0) return str1.substring(str2.length()); return str1; } // left remove i substrings public static String ltrim(String str1, String str2, int i) { String s1, s2; if (i == 0) i = 1024; s1 = str1; s2 = str1; for (int j=0; j<i; j++) { s2 = ltrim(s1, str2); if (s1 == s2) break; s1 = s2; } return s2; } // right remove a substring public static String rtrim(String str1, String str2) { return rtrim(str1, str2, 1); } // right remove i substrings public static String rtrim(String str1, String str2, int i) { String rstr1, rstr2, s1, s2; rstr1 = reverse(str1); rstr2 = reverse(str2); if (i == 0) i = 1024; s1 = rstr1; s2 = rstr1; for (int j=0; j<i; j++) { s2 = ltrim(s1, rstr2); if (s1 == s2) break; s1 = s2; } return reverse(s2); } // left & right remove a substring public static String trim(String str1, String str2) { return rtrim(ltrim(str1, str2), str2); } // left & right remove i substring public static String trim(String str1, String str2, int i) { return rtrim(ltrim(str1, str2, i), str2, i); } }