mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
Merge remote-tracking branch 'origin/v5-dev' into v5-dev
This commit is contained in:
@@ -122,7 +122,7 @@ public class DateTime extends Date {
|
||||
*/
|
||||
public DateTime(Date date) {
|
||||
this(
|
||||
date.getTime(),//
|
||||
date,//
|
||||
(date instanceof DateTime) ? ((DateTime) date).timeZone : TimeZone.getDefault()
|
||||
);
|
||||
}
|
||||
@@ -135,7 +135,7 @@ public class DateTime extends Date {
|
||||
* @since 4.1.2
|
||||
*/
|
||||
public DateTime(Date date, TimeZone timeZone) {
|
||||
this(date.getTime(), timeZone);
|
||||
this(ObjectUtil.defaultIfNull(date, new Date()).getTime(), timeZone);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,6 +211,38 @@ public class DateTime extends Date {
|
||||
this.timeZone = ObjectUtil.defaultIfNull(timeZone, TimeZone.getDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造格式:<br>
|
||||
* <ol>
|
||||
* <li>yyyy-MM-dd HH:mm:ss</li>
|
||||
* <li>yyyy/MM/dd HH:mm:ss</li>
|
||||
* <li>yyyy.MM.dd HH:mm:ss</li>
|
||||
* <li>yyyy年MM月dd日 HH时mm分ss秒</li>
|
||||
* <li>yyyy-MM-dd</li>
|
||||
* <li>yyyy/MM/dd</li>
|
||||
* <li>yyyy.MM.dd</li>
|
||||
* <li>HH:mm:ss</li>
|
||||
* <li>HH时mm分ss秒</li>
|
||||
* <li>yyyy-MM-dd HH:mm</li>
|
||||
* <li>yyyy-MM-dd HH:mm:ss.SSS</li>
|
||||
* <li>yyyyMMddHHmmss</li>
|
||||
* <li>yyyyMMddHHmmssSSS</li>
|
||||
* <li>yyyyMMdd</li>
|
||||
* <li>EEE, dd MMM yyyy HH:mm:ss z</li>
|
||||
* <li>EEE MMM dd HH:mm:ss zzz yyyy</li>
|
||||
* <li>yyyy-MM-dd'T'HH:mm:ss'Z'</li>
|
||||
* <li>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</li>
|
||||
* <li>yyyy-MM-dd'T'HH:mm:ssZ</li>
|
||||
* <li>yyyy-MM-dd'T'HH:mm:ss.SSSZ</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param dateStr Date字符串
|
||||
* @since 5.6.2
|
||||
*/
|
||||
public DateTime(CharSequence dateStr) {
|
||||
this(DateUtil.parse(dateStr));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
|
@@ -60,7 +60,6 @@ public class FileTypeUtil {
|
||||
FILE_TYPE_MAP.put("4d546864000000060001", "mid"); // MIDI (mid)
|
||||
FILE_TYPE_MAP.put("526172211a0700cf9073", "rar"); // WinRAR
|
||||
FILE_TYPE_MAP.put("235468697320636f6e66", "ini");
|
||||
FILE_TYPE_MAP.put("504B0304140000000800", "ofd"); // ofd文件 国标版式文件
|
||||
FILE_TYPE_MAP.put("504B03040a0000000000", "jar");
|
||||
FILE_TYPE_MAP.put("504B0304140008000800", "jar");
|
||||
// MS Excel 注意:word、msi 和 excel的文件头一样
|
||||
@@ -140,7 +139,7 @@ public class FileTypeUtil {
|
||||
* <pre>
|
||||
* 1、无法识别类型默认按照扩展名识别
|
||||
* 2、xls、doc、msi头信息无法区分,按照扩展名区分
|
||||
* 3、zip可能为docx、xlsx、pptx、jar、war头信息无法区分,按照扩展名区分
|
||||
* 3、zip可能为docx、xlsx、pptx、jar、war、ofd头信息无法区分,按照扩展名区分
|
||||
* </pre>
|
||||
* @param in {@link InputStream}
|
||||
* @param filename 文件名
|
||||
@@ -162,7 +161,7 @@ public class FileTypeUtil {
|
||||
typeName = "msi";
|
||||
}
|
||||
} else if ("zip".equals(typeName)) {
|
||||
// zip可能为docx、xlsx、pptx、jar、war等格式,扩展名辅助判断
|
||||
// zip可能为docx、xlsx、pptx、jar、war、ofd等格式,扩展名辅助判断
|
||||
final String extName = FileUtil.extName(filename);
|
||||
if ("docx".equalsIgnoreCase(extName)) {
|
||||
typeName = "docx";
|
||||
@@ -174,6 +173,8 @@ public class FileTypeUtil {
|
||||
typeName = "jar";
|
||||
} else if ("war".equalsIgnoreCase(extName)) {
|
||||
typeName = "war";
|
||||
} else if ("ofd".equalsIgnoreCase(extName)) {
|
||||
typeName = "ofd";
|
||||
}
|
||||
}
|
||||
return typeName;
|
||||
|
@@ -1,19 +1,21 @@
|
||||
package cn.hutool.core.map;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 驼峰Key风格的LinkedHashMap<br>
|
||||
* 对KEY转换为驼峰,get("int_value")和get("intValue")获得的值相同,put进入的值也会被覆盖
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @since 4.0.7
|
||||
*/
|
||||
public class CamelCaseLinkedMap<K, V> extends CamelCaseMap<K, V> {
|
||||
public class CamelCaseLinkedMap<K, V> extends CustomKeyMap<K, V> {
|
||||
private static final long serialVersionUID = 4043263744224569870L;
|
||||
|
||||
// ------------------------------------------------------------------------- Constructor start
|
||||
@@ -26,7 +28,7 @@ public class CamelCaseLinkedMap<K, V> extends CamelCaseMap<K, V> {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param initialCapacity 初始大小
|
||||
*/
|
||||
public CamelCaseLinkedMap(int initialCapacity) {
|
||||
@@ -35,7 +37,7 @@ public class CamelCaseLinkedMap<K, V> extends CamelCaseMap<K, V> {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param m Map
|
||||
*/
|
||||
public CamelCaseLinkedMap(Map<? extends K, ? extends V> m) {
|
||||
@@ -44,7 +46,7 @@ public class CamelCaseLinkedMap<K, V> extends CamelCaseMap<K, V> {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param loadFactor 加载因子
|
||||
* @param m Map
|
||||
*/
|
||||
@@ -55,7 +57,7 @@ public class CamelCaseLinkedMap<K, V> extends CamelCaseMap<K, V> {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param initialCapacity 初始大小
|
||||
* @param loadFactor 加载因子
|
||||
*/
|
||||
@@ -63,4 +65,18 @@ public class CamelCaseLinkedMap<K, V> extends CamelCaseMap<K, V> {
|
||||
super(new LinkedHashMap<>(initialCapacity, loadFactor));
|
||||
}
|
||||
// ------------------------------------------------------------------------- Constructor end
|
||||
|
||||
/**
|
||||
* 将Key转为驼峰风格,如果key为字符串的话
|
||||
*
|
||||
* @param key KEY
|
||||
* @return 驼峰Key
|
||||
*/
|
||||
@Override
|
||||
protected Object customKey(Object key) {
|
||||
if (key instanceof CharSequence) {
|
||||
key = StrUtil.toCamelCase(key.toString());
|
||||
}
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
@@ -6,14 +6,14 @@ import java.util.Map;
|
||||
/**
|
||||
* 忽略大小写的LinkedHashMap<br>
|
||||
* 对KEY忽略大小写,get("Value")和get("value")获得的值相同,put进入的值也会被覆盖
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @since 3.3.1
|
||||
*/
|
||||
public class CaseInsensitiveLinkedMap<K, V> extends CaseInsensitiveMap<K, V> {
|
||||
public class CaseInsensitiveLinkedMap<K, V> extends CustomKeyMap<K, V> {
|
||||
private static final long serialVersionUID = 4043263744224569870L;
|
||||
|
||||
// ------------------------------------------------------------------------- Constructor start
|
||||
@@ -26,7 +26,7 @@ public class CaseInsensitiveLinkedMap<K, V> extends CaseInsensitiveMap<K, V> {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param initialCapacity 初始大小
|
||||
*/
|
||||
public CaseInsensitiveLinkedMap(int initialCapacity) {
|
||||
@@ -35,7 +35,7 @@ public class CaseInsensitiveLinkedMap<K, V> extends CaseInsensitiveMap<K, V> {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param m Map
|
||||
*/
|
||||
public CaseInsensitiveLinkedMap(Map<? extends K, ? extends V> m) {
|
||||
@@ -44,7 +44,7 @@ public class CaseInsensitiveLinkedMap<K, V> extends CaseInsensitiveMap<K, V> {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param loadFactor 加载因子
|
||||
* @param m Map
|
||||
* @since 3.1.2
|
||||
@@ -56,7 +56,7 @@ public class CaseInsensitiveLinkedMap<K, V> extends CaseInsensitiveMap<K, V> {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param initialCapacity 初始大小
|
||||
* @param loadFactor 加载因子
|
||||
*/
|
||||
@@ -64,4 +64,18 @@ public class CaseInsensitiveLinkedMap<K, V> extends CaseInsensitiveMap<K, V> {
|
||||
super(new LinkedHashMap<>(initialCapacity, loadFactor));
|
||||
}
|
||||
// ------------------------------------------------------------------------- Constructor end
|
||||
|
||||
/**
|
||||
* 将Key转为小写
|
||||
*
|
||||
* @param key KEY
|
||||
* @return 小写KEY
|
||||
*/
|
||||
@Override
|
||||
protected Object customKey(Object key) {
|
||||
if (key instanceof CharSequence) {
|
||||
key = key.toString().toLowerCase();
|
||||
}
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
@@ -128,7 +128,7 @@ public class UrlPath {
|
||||
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
for (String segment : segments) {
|
||||
builder.append(CharUtil.SLASH).append(URLUtil.encodeAll(segment, charset));
|
||||
builder.append(CharUtil.SLASH).append(URLUtil.encode(segment, charset));
|
||||
}
|
||||
if (withEngTag || StrUtil.isEmpty(builder)) {
|
||||
builder.append(CharUtil.SLASH);
|
||||
|
@@ -54,6 +54,30 @@ public class UnicodeUtil {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符编码为Unicode形式
|
||||
*
|
||||
* @param c 被编码的字符
|
||||
* @return Unicode字符串
|
||||
* @since 5.6.2
|
||||
* @see HexUtil#toUnicodeHex(char)
|
||||
*/
|
||||
public static String toUnicode(char c) {
|
||||
return HexUtil.toUnicodeHex(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符编码为Unicode形式
|
||||
*
|
||||
* @param c 被编码的字符
|
||||
* @return Unicode字符串
|
||||
* @since 5.6.2
|
||||
* @see HexUtil#toUnicodeHex(int)
|
||||
*/
|
||||
public static String toUnicode(int c) {
|
||||
return HexUtil.toUnicodeHex(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串编码为Unicode形式
|
||||
*
|
||||
|
@@ -89,7 +89,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
* @return 是否为非空
|
||||
*/
|
||||
public static <T> boolean isNotEmpty(T[] array) {
|
||||
return (array != null && array.length != 0);
|
||||
return (null != array && array.length != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -653,7 +653,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public static <T extends CharSequence> T[] removeEmpty(T[] array) {
|
||||
return filter(array, (Filter<T>) t -> false == StrUtil.isEmpty(t));
|
||||
return filter(array, StrUtil::isNotEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -665,7 +665,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public static <T extends CharSequence> T[] removeBlank(T[] array) {
|
||||
return filter(array, (Filter<T>) t -> false == StrUtil.isBlank(t));
|
||||
return filter(array, StrUtil::isNotBlank);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -900,11 +900,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
* @return 是否为数组对象,如果为{@code null} 返回false
|
||||
*/
|
||||
public static boolean isArray(Object obj) {
|
||||
if (null == obj) {
|
||||
// throw new NullPointerException("Object check for isArray is null");
|
||||
return false;
|
||||
}
|
||||
return obj.getClass().isArray();
|
||||
return null != obj && obj.getClass().isArray();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1227,35 +1223,39 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
* @return 连接后的字符串
|
||||
*/
|
||||
public static String join(Object array, CharSequence conjunction) {
|
||||
if (isArray(array)) {
|
||||
final Class<?> componentType = array.getClass().getComponentType();
|
||||
if (componentType.isPrimitive()) {
|
||||
final String componentTypeName = componentType.getName();
|
||||
switch (componentTypeName) {
|
||||
case "long":
|
||||
return join((long[]) array, conjunction);
|
||||
case "int":
|
||||
return join((int[]) array, conjunction);
|
||||
case "short":
|
||||
return join((short[]) array, conjunction);
|
||||
case "char":
|
||||
return join((char[]) array, conjunction);
|
||||
case "byte":
|
||||
return join((byte[]) array, conjunction);
|
||||
case "boolean":
|
||||
return join((boolean[]) array, conjunction);
|
||||
case "float":
|
||||
return join((float[]) array, conjunction);
|
||||
case "double":
|
||||
return join((double[]) array, conjunction);
|
||||
default:
|
||||
throw new UtilException("Unknown primitive type: [{}]", componentTypeName);
|
||||
}
|
||||
} else {
|
||||
return join((Object[]) array, conjunction);
|
||||
}
|
||||
if(null == array){
|
||||
throw new NullPointerException("Array must be not null!");
|
||||
}
|
||||
if (false == isArray(array)) {
|
||||
throw new IllegalArgumentException(StrUtil.format("[{}] is not a Array!", array.getClass()));
|
||||
}
|
||||
|
||||
final Class<?> componentType = array.getClass().getComponentType();
|
||||
if (componentType.isPrimitive()) {
|
||||
final String componentTypeName = componentType.getName();
|
||||
switch (componentTypeName) {
|
||||
case "long":
|
||||
return join((long[]) array, conjunction);
|
||||
case "int":
|
||||
return join((int[]) array, conjunction);
|
||||
case "short":
|
||||
return join((short[]) array, conjunction);
|
||||
case "char":
|
||||
return join((char[]) array, conjunction);
|
||||
case "byte":
|
||||
return join((byte[]) array, conjunction);
|
||||
case "boolean":
|
||||
return join((boolean[]) array, conjunction);
|
||||
case "float":
|
||||
return join((float[]) array, conjunction);
|
||||
case "double":
|
||||
return join((double[]) array, conjunction);
|
||||
default:
|
||||
throw new UtilException("Unknown primitive type: [{}]", componentTypeName);
|
||||
}
|
||||
} else {
|
||||
return join((Object[]) array, conjunction);
|
||||
}
|
||||
throw new UtilException(StrUtil.format("[{}] is not a Array!", array.getClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1266,7 +1266,9 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
* @since 3.0.1
|
||||
*/
|
||||
public static byte[] toArray(ByteBuffer bytebuffer) {
|
||||
if (false == bytebuffer.hasArray()) {
|
||||
if (bytebuffer.hasArray()) {
|
||||
return Arrays.copyOfRange(bytebuffer.array(), bytebuffer.position(), bytebuffer.limit());
|
||||
} else {
|
||||
int oldPosition = bytebuffer.position();
|
||||
bytebuffer.position(0);
|
||||
int size = bytebuffer.limit();
|
||||
@@ -1274,8 +1276,6 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
bytebuffer.get(buffers);
|
||||
bytebuffer.position(oldPosition);
|
||||
return buffers;
|
||||
} else {
|
||||
return Arrays.copyOfRange(bytebuffer.array(), bytebuffer.position(), bytebuffer.limit());
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -366,4 +366,47 @@ public class CharUtil {
|
||||
public static int digit16(int b) {
|
||||
return Character.digit(b, 16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字母、数字转换为带圈的字符:
|
||||
* <pre>
|
||||
* '1' -》 '①'
|
||||
* 'A' -》 'Ⓐ'
|
||||
* 'a' -》 'ⓐ'
|
||||
* </pre>
|
||||
*
|
||||
* @param c 被转换的字符,如果字符不支持转换,返回原字符
|
||||
* @return 转换后的字符
|
||||
* @since 5.6.2
|
||||
*/
|
||||
public static char toCloseChar(char c){
|
||||
int result = c;
|
||||
if(c >='1' && c <= '9'){
|
||||
result = '①' + c - '1';
|
||||
} else if(c >='A' && c <= 'Z'){
|
||||
result = 'Ⓐ' + c - 'A';
|
||||
} else if(c >='a' && c <= 'z'){
|
||||
result = 'ⓐ' + c - 'a';
|
||||
}
|
||||
return (char) result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将[1-20]数字转换为带圈的字符:
|
||||
* <pre>
|
||||
* 1 -》 '①'
|
||||
* 12 -》 '⑫'
|
||||
* 20 -》 '⑳'
|
||||
* </pre>
|
||||
*
|
||||
* @param number 被转换的数字
|
||||
* @return 转换后的字符
|
||||
* @since 5.6.2
|
||||
*/
|
||||
public static char toCloseByNumber(int number){
|
||||
if(number > 20){
|
||||
throw new IllegalArgumentException("Number must be [1-20]");
|
||||
}
|
||||
return (char) ('①' + number - 1);
|
||||
}
|
||||
}
|
||||
|
@@ -57,11 +57,12 @@ public class DesensitizedUtil {
|
||||
* DesensitizedUtil.desensitized("duandazhi-jack@gmail.com.cn", DesensitizedUtils.DesensitizedType.EMAIL)) = "d*************@gmail.com.cn"
|
||||
* DesensitizedUtil.desensitized("1234567890", DesensitizedUtils.DesensitizedType.PASSWORD)) = "**********"
|
||||
* DesensitizedUtil.desensitized("苏D40000", DesensitizedUtils.DesensitizedType.CAR_LICENSE)) = "苏D4***0"
|
||||
* </pre>
|
||||
*
|
||||
* @author dazer and neusoft and qiaomu
|
||||
* @param str 字符串
|
||||
* @param str 字符串
|
||||
* @param desensitizedType 脱敏类型;可以脱敏:用户id、中文名、身份证号、座机号、手机号、地址、电子邮件、密码
|
||||
* @return 脱敏之后的字符串
|
||||
* @author dazer and neusoft and qiaomu
|
||||
* @since 5.6.2
|
||||
*/
|
||||
public static String desensitized(CharSequence str, DesensitizedUtil.DesensitizedType desensitizedType) {
|
||||
@@ -77,7 +78,7 @@ public class DesensitizedUtil {
|
||||
newStr = DesensitizedUtil.chineseName(String.valueOf(str));
|
||||
break;
|
||||
case ID_CARD:
|
||||
newStr = DesensitizedUtil.idCardNum(String.valueOf(str),1,2);
|
||||
newStr = DesensitizedUtil.idCardNum(String.valueOf(str), 1, 2);
|
||||
break;
|
||||
case FIXED_PHONE:
|
||||
newStr = DesensitizedUtil.fixedPhone(String.valueOf(str));
|
||||
@@ -159,7 +160,7 @@ public class DesensitizedUtil {
|
||||
if (StrUtil.isBlank(num)) {
|
||||
return StrUtil.EMPTY;
|
||||
}
|
||||
return StrUtil.hide(num, 4, num.length()-2);
|
||||
return StrUtil.hide(num, 4, num.length() - 2);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -172,7 +173,7 @@ public class DesensitizedUtil {
|
||||
if (StrUtil.isBlank(num)) {
|
||||
return StrUtil.EMPTY;
|
||||
}
|
||||
return StrUtil.hide(num, 3, num.length()-4);
|
||||
return StrUtil.hide(num, 3, num.length() - 4);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,11 +188,11 @@ public class DesensitizedUtil {
|
||||
return StrUtil.EMPTY;
|
||||
}
|
||||
int length = address.length();
|
||||
return StrUtil.hide(address, length-sensitiveSize, length);
|
||||
return StrUtil.hide(address, length - sensitiveSize, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 【电子邮箱】邮箱前缀仅显示第一个字母,前缀其他隐藏,用星号代替,@及后面的地址显示,比如:d**@126.com>
|
||||
* 【电子邮箱】邮箱前缀仅显示第一个字母,前缀其他隐藏,用星号代替,@及后面的地址显示,比如:d**@126.com
|
||||
*
|
||||
* @param email 邮箱
|
||||
* @return 脱敏后的邮箱
|
||||
@@ -222,11 +223,11 @@ public class DesensitizedUtil {
|
||||
|
||||
/**
|
||||
* 【中国车牌】车牌中间用*代替
|
||||
* eg1:null => ""
|
||||
* eg1:"" => ""
|
||||
* eg3:苏D40000 => 苏D4***0
|
||||
* eg4:陕A12345D => 陕A1****D
|
||||
* eg5:京A123 => 京A123 如果是错误的车牌,不处理
|
||||
* eg1:null -》 ""
|
||||
* eg1:"" -》 ""
|
||||
* eg3:苏D40000 -》 苏D4***0
|
||||
* eg4:陕A12345D -》 陕A1****D
|
||||
* eg5:京A123 -》 京A123 如果是错误的车牌,不处理
|
||||
*
|
||||
* @param carLicense 完整的车牌号
|
||||
* @return 脱敏后的车牌
|
||||
|
@@ -262,7 +262,7 @@ public class HexUtil {
|
||||
* 转换的字符串如果u后不足4位,则前面用0填充,例如:
|
||||
*
|
||||
* <pre>
|
||||
* '我' =》\u4f60
|
||||
* '你' =》\u4f60
|
||||
* </pre>
|
||||
*
|
||||
* @param value int值,也可以是char
|
||||
@@ -287,7 +287,7 @@ public class HexUtil {
|
||||
* 转换的字符串如果u后不足4位,则前面用0填充,例如:
|
||||
*
|
||||
* <pre>
|
||||
* '我' =》\u4f60
|
||||
* '你' =》'\u4f60'
|
||||
* </pre>
|
||||
*
|
||||
* @param ch char值
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.collection;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.lang.Editor;
|
||||
import cn.hutool.core.lang.Filter;
|
||||
|
@@ -5,7 +5,7 @@ import org.junit.Test;
|
||||
|
||||
/**
|
||||
* DateTime单元测试
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
@@ -32,6 +32,27 @@ public class DateTimeTest {
|
||||
Assert.assertEquals(5, day);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void datetimeTest2() {
|
||||
DateTime dateTime = new DateTime("2017-01-05 12:34:23");
|
||||
|
||||
// 年
|
||||
int year = dateTime.year();
|
||||
Assert.assertEquals(2017, year);
|
||||
|
||||
// 季度(非季节)
|
||||
Quarter season = dateTime.quarterEnum();
|
||||
Assert.assertEquals(Quarter.Q1, season);
|
||||
|
||||
// 月份
|
||||
Month month = dateTime.monthEnum();
|
||||
Assert.assertEquals(Month.JANUARY, month);
|
||||
|
||||
// 日
|
||||
int day = dateTime.dayOfMonth();
|
||||
Assert.assertEquals(5, day);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void quarterTest() {
|
||||
DateTime dateTime = new DateTime("2017-01-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
|
||||
@@ -90,7 +111,7 @@ public class DateTimeTest {
|
||||
int month = DateUtil.parse("2017-07-01").month();
|
||||
Assert.assertEquals(6, month);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void weekOfYearTest() {
|
||||
DateTime date = DateUtil.parse("2016-12-27");
|
||||
|
@@ -235,4 +235,12 @@ public class UrlBuilderTest {
|
||||
final UrlBuilder urlBuilder = UrlBuilder.of(webUrl, StandardCharsets.UTF_8);
|
||||
Assert.assertEquals("a=123&b=4%3F6&c=789", urlBuilder.getQueryStr());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodePathTest(){
|
||||
// Path中的某些符号无需转义,比如=
|
||||
final String urlStr = "http://hq.sinajs.cn/list=sh600519";
|
||||
final UrlBuilder urlBuilder = UrlBuilder.ofHttp(urlStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals(urlStr, urlBuilder.toString());
|
||||
}
|
||||
}
|
||||
|
@@ -53,6 +53,12 @@ public class ArrayUtilTest {
|
||||
public void isNotEmptyTest() {
|
||||
int[] a = {1, 2};
|
||||
Assert.assertTrue(ArrayUtil.isNotEmpty(a));
|
||||
|
||||
String[] b = {"a", "b", "c"};
|
||||
Assert.assertTrue(ArrayUtil.isNotEmpty(b));
|
||||
|
||||
Object c = new Object[]{"1", "2", 3, 4D};
|
||||
Assert.assertTrue(ArrayUtil.isNotEmpty(c));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -253,15 +259,19 @@ public class ArrayUtilTest {
|
||||
String[] array = {"aa", "bb", "cc", "dd"};
|
||||
String join = ArrayUtil.join(array, ",", "[", "]");
|
||||
Assert.assertEquals("[aa],[bb],[cc],[dd]", join);
|
||||
|
||||
Object array2 = new String[]{"aa", "bb", "cc", "dd"};
|
||||
String join2 = ArrayUtil.join(array2, ",");
|
||||
Assert.assertEquals("aa,bb,cc,dd", join2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getArrayTypeTest() {
|
||||
Class<?> arrayType = ArrayUtil.getArrayType(int.class);
|
||||
Assert.assertEquals(int[].class, arrayType);
|
||||
Assert.assertSame(int[].class, arrayType);
|
||||
|
||||
arrayType = ArrayUtil.getArrayType(String.class);
|
||||
Assert.assertEquals(String[].class, arrayType);
|
||||
Assert.assertSame(String[].class, arrayType);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -384,4 +394,34 @@ public class ArrayUtilTest {
|
||||
final int[] reverse = ArrayUtil.reverse(a);
|
||||
Assert.assertArrayEquals(new int[]{4,3,2,1}, reverse);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeEmptyTest() {
|
||||
String[] a = {"a", "b", "", null, " ", "c"};
|
||||
String[] resultA = {"a", "b", " ", "c"};
|
||||
Assert.assertArrayEquals(ArrayUtil.removeEmpty(a), resultA);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeBlankTest() {
|
||||
String[] a = {"a", "b", "", null, " ", "c"};
|
||||
String[] resultA = {"a", "b", "c"};
|
||||
Assert.assertArrayEquals(ArrayUtil.removeBlank(a), resultA);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullToEmptyTest() {
|
||||
String[] a = {"a", "b", "", null, " ", "c"};
|
||||
String[] resultA = {"a", "b", "", "", " ", "c"};
|
||||
Assert.assertArrayEquals(ArrayUtil.nullToEmpty(a), resultA);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrapTest() {
|
||||
Object a = new int[]{1, 2, 3, 4};
|
||||
Object[] wrapA = ArrayUtil.wrap(a);
|
||||
for (Object o : wrapA) {
|
||||
Assert.assertTrue(o instanceof Integer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CharUtilTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void trimTest() {
|
||||
//此字符串中的第一个字符为不可见字符: '\u202a'
|
||||
@@ -12,13 +12,13 @@ public class CharUtilTest {
|
||||
Assert.assertEquals('\u202a', str.charAt(0));
|
||||
Assert.assertTrue(CharUtil.isBlankChar(str.charAt(0)));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isEmojiTest() {
|
||||
String a = "莉🌹";
|
||||
Assert.assertFalse(CharUtil.isEmoji(a.charAt(0)));
|
||||
Assert.assertTrue(CharUtil.isEmoji(a.charAt(1)));
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -38,4 +38,18 @@ public class CharUtilTest {
|
||||
char a3 = '\u3000';
|
||||
Assert.assertTrue(CharUtil.isBlankChar(a3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toCloseCharTest(){
|
||||
Assert.assertEquals('②', CharUtil.toCloseChar('2'));
|
||||
Assert.assertEquals('Ⓜ', CharUtil.toCloseChar('M'));
|
||||
Assert.assertEquals('ⓡ', CharUtil.toCloseChar('r'));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toCloseByNumberTest(){
|
||||
Assert.assertEquals('②', CharUtil.toCloseByNumber(2));
|
||||
Assert.assertEquals('⑫', CharUtil.toCloseByNumber(12));
|
||||
Assert.assertEquals('⑳', CharUtil.toCloseByNumber(20));
|
||||
}
|
||||
}
|
||||
|
@@ -9,26 +9,26 @@ import org.junit.Test;
|
||||
*
|
||||
*/
|
||||
public class HexUtilTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void hexStrTest(){
|
||||
String str = "我是一个字符串";
|
||||
|
||||
|
||||
String hex = HexUtil.encodeHexStr(str, CharsetUtil.CHARSET_UTF_8);
|
||||
String decodedStr = HexUtil.decodeHexStr(hex);
|
||||
|
||||
|
||||
Assert.assertEquals(str, decodedStr);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void toUnicodeHexTest() {
|
||||
String unicodeHex = HexUtil.toUnicodeHex('\u2001');
|
||||
Assert.assertEquals("\\u2001", unicodeHex);
|
||||
|
||||
|
||||
unicodeHex = HexUtil.toUnicodeHex('你');
|
||||
Assert.assertEquals("\\u4f60", unicodeHex);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isHexNumberTest() {
|
||||
String a = "0x3544534F444";
|
||||
|
Reference in New Issue
Block a user