官方文档
HuTool封装了很多的工具类,听说很好使,今天咱来试试。
/** 转成字符串 */
@Test
public void m1() {
// int转成字符串
int a = 1;
String aStr = Convert.toStr(a);
// String aStr = a + ""; // 跟上面效果是一样的, 但是人家参数是 Object类型的
System.out.println("aStr= " + aStr);
System.out.println("aStr.getClass() = " + aStr.getClass());
// 数组转成字符串
long[] b = {1,2,3,4,5};
//bStr为:"[1, 2, 3, 4, 5]"
String bStr = Convert.toStr(b);
System.out.println("bStr= " + bStr);
System.out.println("Arrays.toString(b)= " + Arrays.toString(b));
Map<String, String> map = new LinkedHashMap<String, String>(){
private static final long serialVersionUID = 1L;
{
put("水木年华", "因为梦见你离开, 我从哭泣中醒来, 看夜风吹过窗台.");
put("在雨中", "在雨中, 看见你的身影, 那么悲伤, 那么疯狂 刹那间, 往事涌上心头, 时光飞逝, 掉进了回忆.");
}
};
System.out.println("map = " + map);
System.out.println("Convert.toStr(map)= " + Convert.toStr(map));
}
/** 测试 转成字符串效率 */
@Test
public void m2() {
int a = 1;
long begin = System.currentTimeMillis();
Convert.toStr(a);
long end = System.currentTimeMillis();
System.out.println(begin);
System.out.println(end);
System.out.println(end - begin); // 40-150 ms
begin = System.currentTimeMillis();
String aStr = a + "";
end = System.currentTimeMillis();
System.out.println(begin);
System.out.println(end); // 稳定在 0ms
}
/** 数组 转换成 指定类型数组 */
@Test
public void m3() {
String[] b = { "1", "2", "3", "4" };
long begin = System.currentTimeMillis();
//结果为Integer数组
Integer[] intArray = Convert.toIntArray(b);
long end = System.currentTimeMillis();
System.out.println("HuTool耗时: " + (end - begin) + "ms"); // 65ms
System.out.println(intArray.getClass());
System.out.println("-------------------------------------------------");
begin = System.currentTimeMillis();
List<Integer> intList = new ArrayList<>();
for (int i = 0; i < b.length; i++) {
intList.add(Integer.valueOf(b[i]));
}
Integer[] intArrayPeove = new Integer[intList.size()];
intList.toArray(intArrayPeove);
end = System.currentTimeMillis();
System.out.println("Peove耗时: " + (end - begin) + "ms"); // 0ms
System.out.println(intArrayPeove.getClass());
System.out.println(Convert.toStr(intArrayPeove));
}
/** 字符串 转成 日期对象 */
@Test
public void m4() throws ParseException {
String a = "2020-07-04";
long begin = System.currentTimeMillis();
Date value = Convert.toDate(a);
long end = System.currentTimeMillis();
System.out.println("HuTool耗时: " + (end - begin) + "ms"); // 653ms
System.out.println("value = " + value);
begin = System.currentTimeMillis();
Date valuePeove = new SimpleDateFormat("yyyy-MM-dd").parse(a);
end = System.currentTimeMillis();
System.out.println("Peove耗时: " + (end - begin) + "ms"); // 9ms
System.out.println("valuePeove = " + valuePeove);
}
/** 半角和全角的转换 */
@Test
public void m5() {
String a = "123456789";
long begin = System.currentTimeMillis();
String sbc = Convert.toSBC(a);
long end = System.currentTimeMillis();
System.out.println("HuTool耗时: " + (end - begin) + "ms"); // 1-2ms
System.out.println("sbc = " + sbc); // 结果为:"123456789"
}
/**
* 财务类需求: 将小写的金额 转换成 大写的金额
*
* 注意 转换为大写只能精确到分(小数点儿后两位),之后的数字会被忽略。
*/
@Test
public void m7() {
double a = 67556.326888;
long begin = System.currentTimeMillis();
String digitUppercase = Convert.digitToChinese(a);
long end = System.currentTimeMillis();
System.out.println("HuTool耗时: " + (end - begin) + "ms"); // 差不多 4-30ms
System.out.println("digitUppercase = " + digitUppercase); // 结果为:"陆万柒仟伍佰伍拾陆元叁角贰分"
begin = System.currentTimeMillis();
String digitUppercasePeove = TotalUtils.digitToChinese(a);
end = System.currentTimeMillis();
System.out.println("peove耗时: " + (end - begin) + "ms"); // 差不多 4-30ms
System.out.println("digitUppercasePeove = " + digitUppercasePeove); // 结果为:"陆万柒仟伍佰伍拾陆元叁角贰分"
}
/** 16进制的转换 */
@Test
public void m6() throws UnsupportedEncodingException {
String a = "我是一个小小的可爱的字符串";
long begin = System.currentTimeMillis();
String hex = Convert.toHex(a, CharsetUtil.CHARSET_UTF_8);
long end = System.currentTimeMillis();
System.out.println("HuTool耗时: " + (end - begin) + "ms"); // 15-63ms
System.out.println("hex = " + hex); // 结果:"e68891e698afe4b880e4b8aae5b08fe5b08fe79a84e58fafe788b1e79a84e5ad97e7aca6e4b8b2"
begin = System.currentTimeMillis();
String hexStr = Convert.hexToStr(hex, CharsetUtil.CHARSET_UTF_8); // since 4.1.11
end = System.currentTimeMillis();
System.out.println("HuTool耗时: " + (end - begin) + "ms"); // 0ms
System.out.println("hexStr = " + hexStr); // hexStr = 我是一个小小的可爱的字符串
begin = System.currentTimeMillis();
String hexPeove = TotalUtils.str2HexStr(a);
end = System.currentTimeMillis();
System.out.println("Peove耗时: " + (end - begin) + "ms"); // 0ms, 写到其他包 然后静态引入 现在 1ms了.
System.out.println("hexPeove = " + hexPeove);
begin = System.currentTimeMillis();
String hexStrPeove = TotalUtils.hexStr2Str(hexPeove);
end = System.currentTimeMillis();
System.out.println("Peove耗时: " + (end - begin) + "ms"); // 0ms
System.out.println("hexStrPeove = " + hexStrPeove); // hexStrPeove = 我是一个小小的可爱的字符串
}
/*
* ------------------------------------------------------------------
* + 还是 LocalDateTime 好用. 再往下的就不看了. +
* + 网址: https://www.hutool.cn 核心/日期时间 +
* + +
* + 最后 “其他” 里的计算年龄 有点意思, 自己也实现一个. +
* + 工具类的效率太慢了.. 自己写的工具类 只设定自己需要的, 效率快很多. +
* ------------------------------------------------------------------
*/
LocalDateTime的使用
/**
* 获取当前时间, 这个工具类给封装好 格式了.
*
* 其实返回的是 dateTime, 提供了各种类用于操作日期和时间,该模块侧重于高效率的格式化输出
*/
@Test
public void m1() {
//当前时间, 其实返回的是 dateTime, 提供了各种类用于操作日期和时间,该模块侧重于高效率的格式化输出
Date date1 = DateUtil.date(System.currentTimeMillis());
//当前时间字符串,格式:yyyy-MM-dd HH:mm:ss
String now = DateUtil.now();
//当前日期字符串,格式:yyyy-MM-dd
String today= DateUtil.today();
System.out.println("date1 = " + date1); // date1 = 2020-07-10 09:24:23
System.out.println("now = " + now); // now = 2020-07-10 09:24:23
System.out.println("today = " + today); // today = 2020-07-10
DateTime dateTime1 = new DateTime();
DateTime dateTime2 = new DateTime(new Date());
DateTime dateTime3 = new DateTime(System.currentTimeMillis());
System.out.println("dateTime1 = " + dateTime1); // dateTime = 2020-07-10 09:33:00
System.out.println("dateTime2 = " + dateTime2); // dateTime = 2020-07-10 09:33:00
System.out.println("dateTime3 = " + dateTime3); // dateTime = 2020-07-10 09:33:00
}
/**
* 字符串转日期: 返回的DateTime(一种时间格式化的输出)
*
* DateUtil.parse方法会自动识别一些常用格式,包括:
*
* yyyy-MM-dd HH:mm:ss
* yyyy-MM-dd
* HH:mm:ss
* yyyy-MM-dd HH:mm
* yyyy-MM-dd HH:mm:ss.SSS 等等
*/
@Test
public void m2() {
String dateStr = "1997-7-14";
/*
* 如果遇到了20种格式里没有的(比如2020710), 还会报错, 还是要处理异常, 这里也没有提示 try-catch.
* 异常能不能 不要自己处理.... 向上抛出啊!!
*/
DateTime dateTime = DateUtil.parse(dateStr);
System.out.println("dateTime = " + dateTime); // dateTime = 1997-07-14 00:00:00
dateStr = "19971314";
dateTime = DateUtil.parse(dateStr, "yyyyMMdd");
System.out.println("dateTime = " + dateTime); // dateTime = 1998-01-14 00:00:00
}
/** 格式化日期输出: Date 转换成 String. */
@Test
public void m3() {
String dateStr = "20200710";
Date date = DateUtil.parse(dateStr);
System.out.println("date = " + date); // date = 2020-07-10 00:00:00
String dateStr1 = DateUtil.format(date, "yyyyMMdd");
System.out.println("dateStr1 = " + dateStr1); // dateStr1 = 20200710
//常用格式的格式化
String dateStr2 = DateUtil.formatDate(date);
//结果:2020-07-10 00:00:00
String formatDateTime = DateUtil.formatDateTime(date);
//结果:00:00:00
String formatTime = DateUtil.formatTime(date);
System.out.println("dateStr2 = " + dateStr2); // dateStr2 = 2020-07-10
System.out.println("formatDateTime = " + formatDateTime); // formatDateTime = 2020-07-10 00:00:00
System.out.println("formatTime = " + formatTime); // formatTime = 00:00:00
}
/** 计算年龄 */
@Test
public void m4() {
String ageStr = "1997-04-02 17:10:52";
long begin = System.currentTimeMillis();
int age = DateUtil.ageOfNow(ageStr);
long end = System.currentTimeMillis();
System.out.println("age = " + age); // age = 23... 这个时间不准. 下面自己写的工具类 还带了 天数判断呢.
System.out.println("[Hutool] 耗时: " + (end - begin) + "ms"); // [Hutool] 耗时: 1025ms
begin = System.currentTimeMillis();
int age1 = TotalUtils.getAgeByBirth(ageStr);
end = System.currentTimeMillis();
System.out.println("age1 = " + age1); // age1 = 24, oc 24了么....
System.out.println("[peove] 耗时: " + (end - begin) + "ms"); // [peove] 耗时: 22ms
Calendar now = Calendar.getInstance();
int dayOfYear = now.get(Calendar.DAY_OF_YEAR);
System.out.println("now = " + now); // 有timestamp、zone 等等.
System.out.println("dayOfYear = " + dayOfYear); // dayOfYear = 192 表示 一年的第多少天
}