Java中特别实用的类型转换工具,涵盖范围较广、功能强大,并且能够自定义!!!

3,140 阅读5分钟

我们在平时的开发中,总是会遇到各种各样的类型相互转换,让人看到发愁,此工具就是为了解决这个痛点的存在,即使是没有涵盖在内的类型转换,这边也提供了类型转换注册器。非常的方便。

因此才写下了这篇文章。🤡🐱‍🏍🐱‍💻🐲🐉🐟👯‍♀️🐇

最近迷上了这众小表情🤣

地点: 不知道🤦‍

作者:

一、hutool工具介绍

Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。😀

Hutool中的工具方法来自每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当。

总结起来就是对Java的复杂操作再进行了一次封装。(果然没有什么是加一层不能解决的啊,一层不行就加两层🐕)

导入依赖:

 <dependency>
     <groupId>cn.hutool</groupId>
     <artifactId>hutool-all</artifactId>
     <version>5.6.5</version>
 </dependency>

:也可根据自己项目需求导入具体的hutool jar包 ,此处的 hutool-all 是导入了所有相关的hutool jar。

我才不会讲我是为了偷懒才这么做的勒"🐕保命")

二、Java常见类型转换

2.1、转换为字符串

 /**
 * 1、转换为字符串
 */
 @Test
 public void testString() {
     int a = 1;
     //aStr为"1"
     String aStr = Convert.toStr(a);
     System.out.println(aStr);
     long[] b = {1, 2, 3, 4, 5};
     //bStr为:"[1, 2, 3, 4, 5]"
     String bStr = Convert.toStr(b);
     System.out.println(bStr);
 }

2.2、转换为指定类型数组

   @Test
     public void testArray() {
         String[] b = {"1", "2", "3", "4"};
         //结果为Integer数组
         Integer[] intArray = Convert.toIntArray(b);
         for (Integer integer : intArray) {
             System.out.print(integer+" ");
         }
         if(intArray instanceof Integer[]){
             System.out.println("转换是成功的 ===>true");
         }
         // 输出结果: 1 2 3 4 转换是成功的 ===>true
         System.out.println();
 //=======long 类型数组转为 Integer类型数组===================================       
         long[] c = {1, 2, 3, 4, 5};
         //结果为Integer数组
         Integer[] intArray2 = Convert.toIntArray(c);
         for (Integer integer : intArray2) {
             System.out.print(integer+" ");
         }
         if(intArray2 instanceof Integer[]){
             System.out.println("转换是成功的 ===>true");
         }
         // 输出结果为:1 2 3 4 5 转换是成功的 ===>true
         System.out.println();
 //=======long 类型数组转为 String 类型数组===================================   
         String[] strings = Convert.toStrArray(c);
         for (String s : strings) {
             System.out.print(s+" ");
         }
 ​
         if(strings instanceof String[]){
             System.out.println("转换是成功的 ===>true");
         }
         // 输出结果为:1 2 3 4 5 转换是成功的 ===>true
         System.out.println();
     }

2.3、日期对象的转换及字符串和日期对象之间的转变

 @Test
 public void testDate(){
     String a = "2017-05-06";
     Date value = Convert.toDate(a);
     if(value instanceof Date){
         //输出为:2017-05-06 00:00:00
         System.out.println(value);
     }
     //========== 字符串类型转换为 LocalDateTime 类型
     LocalDateTime time = Convert.toLocalDateTime(a);
     // 输出:2017-05-06T00:00
     System.out.println(time);
     //==============LocalDateTime 转换为 Date 类型=======
     LocalDateTime now = LocalDateTime.now();
     Date date = Convert.toDate(now);
     // 输出 :2021-07-30 09:45:11
     System.out.println(date);
 }

2.4、转换为集合

 @Test
 public void testCollection(){
     String[] a = {"a", "你", "好", "", "测试"};
     //List<?> list = Convert.toList(a);  如果需要明确指定类型的话 就需要强转
     List<String> list = (List<String>) Convert.toList(a);
     list.forEach(System.out::println);
 ​
 ​
     Integer[] b={1,2,3,4,50};
     //======= 两者只是方式不同  下面这种使用更为广泛=====
     // 第一个参数 集合类型 第二个参数  元素类型  第三个参数就是需要转换的 数据
     Collection<?> objects = Convert.toCollection(List.class, Integer.class, b);
     objects.forEach(System.out::print);
 }

三、其他类型转换

3.1、Unicode和字符串转换

 @Test
 public void testUnicode() {
     String a = "我想屏幕前的你一定非常可爱";
 ​
     //结果为:"\u6211\u60f3\u5c4f\u5e55\u524d\u7684\u4f60\u4e00\u5b9a\u975e\u5e38\u53ef\u7231"
     String unicode = Convert.strToUnicode(a);
     System.out.println(unicode);
     //结果为:"我想屏幕前的你一定非常可爱"
     String raw = Convert.unicodeToStr(unicode);
     System.out.println(raw);
 }

3.2、编码转换

 @Test
 public void testCharset(){
     String a = "我不是乱码";
 ​
     //转换后result为乱码
     String result = Convert.convertCharset(a, CharsetUtil.UTF_8, CharsetUtil.ISO_8859_1);
     // 再通过类型转换 再转换回来 。
     String raw = Convert.convertCharset(result, CharsetUtil.ISO_8859_1, CharsetUtil.UTF_8);
 ​
     // 结果为:我不是乱码
     System.out.println(raw);
 }

这个转换类里面其实特别多的方法,没法一一道来,感兴趣的话,建议直接上手,巨爽真的。

在这里给出一个图哈,大家可以看一下,而且还提供了自定义类型转换器。👶

image-20210730104639588.png

四、自定义类型转换器

上面只是解决了一部分问题,hutool为了完善上述的局限性,就有了自定义转换器。

  • Converter 类型转换接口,通过实现这个接口,重写convert方法,以实现不同类型的对象转换
  • ConverterRegistry 类型转换登记中心。将各种类型Convert对象放入登记中心,通过convert方法查找目标类型对应的转换器,将被转换对象转换之。在此类中,存放着默认转换器自定义转换器,默认转换器是Hutool中预定义的一些转换器,自定义转换器存放用户自定的转换器。

1.自定义转换器

 /**
 * 自定义转换器
 */
 public static class CustomConverter implements Converter<String> {
     @Override
     public String convert(Object value, String defaultValue) throws IllegalArgumentException {
         return "Custom: " + value.toString()+"===我们可以在此处做一些更改";
     }
 }

2.注册转换器

 ConverterRegistry converterRegistry = ConverterRegistry.getInstance();
 //此处做为示例自定义String转换,因为Hutool中已经提供String转换,请尽量不要替换
 //替换可能引发关联转换异常(例如覆盖String转换会影响全局)
 converterRegistry.putCustom(String.class, CustomConverter.class);

3.使用自定义转换器

 int a = 454553;
 String result = converterRegistry.convert(String.class, a);
 // 结果:Custom: 454553===我们可以在此处做一些更改
 System.out.println(result);

五、官方文档 hutool 类型转换

自言自语

一个非常好用的工具类,我只是一个推广的孩子,没有写出什么实质东东,不要怪我哦。

不过hutool真的很好,里面特别多的工具类用起来很爽。

共勉 or🛌