Hello, I'm brother Yao.
Today, I recommend a very easy-to-use Java tool class library. There are basically all enterprise level common tool classes. It can avoid repeated wheel building and save a lot of development time. It is very good and worth understanding and using.
Hutool is homonymous for "confused", which means to pursue the realm of "everything is confused, no loss, no gain".
Hutool is a Java toolkit, just a toolkit. It helps us simplify every line of code, reduce every method, and make the Java language "sweet". Hutool was originally an arrangement of the "util" package in my project. Later, it slowly accumulated and added more non business related functions, widely learned the essence of other open source projects, and finally formed a rich open source tool set after its own arrangement and modification.
1, Function
A Java basic tool class encapsulates JDK methods such as file, stream, encryption and decryption, transcoding, regularization, thread and XML to form various Util tool classes, and provides the following components:
- Hutool AOP JDK dynamic proxy encapsulation provides aspect support under non IOC
- Hutool bloom filter provides bloom filtering of some Hash algorithms
- Hutool cache
- Hutool core, including Bean operation, date, various utils, etc
- Hutool cron timing task module provides timing tasks similar to cronab expressions
- Hutool crypto encryption and decryption module
- The data operation encapsulated by hutool DB JDBC is based on the idea of ActiveRecord
- Hutool DFA multi keyword search based on DFA model
- Hutool extra extension module, which encapsulates the third party (template engine, mail, etc.)
- Hutool Http HTTP Http client encapsulation based on HttpUrlConnection
- Hutool log automatically identifies the log facade of the log implementation
- Hutool script execution encapsulation, such as Javascript
- Hutool Setting features more powerful Setting configuration files and Properties encapsulation
- Hutool system system parameter call encapsulation (JVM information, etc.)
- Hutool JSON JSON implementation
- Implementation of hutool captcha picture verification code
2, Installation
maven project in POM XML add the following dependencies:
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.3</version> </dependency>
3, Simple test
DateUtil
The date time tool class defines some common date time operation methods.
//Conversion between Date, long and Calendar //current time Date date = DateUtil.date(); //Calendar to Date date = DateUtil.date(Calendar.getInstance()); //Timestamp to Date date = DateUtil.date(System.currentTimeMillis()); //Automatic recognition format conversion String dateStr = "2017-03-01"; date = DateUtil.parse(dateStr); //Custom format conversion date = DateUtil.parse(dateStr, "yyyy-MM-dd"); //Format output date String format = DateUtil.format(date, "yyyy-MM-dd"); //Get part of the year int year = DateUtil.year(date); //Get month, count from 0 int month = DateUtil.month(date); //Get the start and end time of a day Date beginOfDay = DateUtil.beginOfDay(date); Date endOfDay = DateUtil.endOfDay(date); //Date and time after offset calculation Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2); //Calculate the offset between date and time long betweenDay = DateUtil.between(date, newDate, DateUnit.DAY);
StrUtil
String tool class, which defines some common string operation methods.
//Determine whether it is an empty string String str = "test"; StrUtil.isEmpty(str); StrUtil.isNotEmpty(str); //Remove the prefix of the string StrUtil.removeSuffix("a.jpg", ".jpg"); StrUtil.removePrefix("a.jpg", "a."); //format string String template = "This is just a placeholder:{}"; String str2 = StrUtil.format(template, "I'm a placeholder"); LOGGER.info("/strUtil format:{}", str2);
NumberUtil
Digital processing tools can be used for addition, subtraction, multiplication and division of various types of numbers and judge types.
double n1 = 1.234; double n2 = 1.234; double result; //Add, subtract, multiply and divide float, double and BigDecimal result = NumberUtil.add(n1, n2); result = NumberUtil.sub(n1, n2); result = NumberUtil.mul(n1, n2); result = NumberUtil.div(n1, n2); //Keep two decimal places BigDecimal roundNum = NumberUtil.round(n1, 2); String n3 = "1.234"; //Judge whether it is a number, integer or floating point number NumberUtil.isNumber(n3); NumberUtil.isInteger(n3); NumberUtil.isDouble(n3); BeanUtil JavaBean Tool class for Map And JavaBean Mutual conversion of objects and copy of object properties. PmsBrand brand = new PmsBrand(); brand.setId(1L); brand.setName("millet"); brand.setShowStatus(0); //Bean to Map Map<String, Object> map = BeanUtil.beanToMap(brand); LOGGER.info("beanUtil bean to map:{}", map); //Map to Bean PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, false); LOGGER.info("beanUtil map to bean:{}", mapBrand); //Bean property copy PmsBrand copyBrand = new PmsBrand(); BeanUtil.copyProperties(brand, copyBrand); LOGGER.info("beanUtil copy properties:{}", copyBrand);
MapUtil
Map operation tool class, which can be used to create map objects and judge whether the map is empty.
//Add multiple key value pairs to the Map Map<Object, Object> map = MapUtil.of(new String[][]{ {"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"} }); //Judge whether the Map is empty MapUtil.isEmpty(map); MapUtil.isNotEmpty(map); AnnotationUtil Annotation tool class, which can be used to obtain the value specified in annotation and annotation. //Gets the annotation list on the specified class, method, field and constructor Annotation[] annotationList = AnnotationUtil.getAnnotations(HutoolController.class, false); LOGGER.info("annotationUtil annotations:{}", annotationList); //Gets the annotation of the specified type Api api = AnnotationUtil.getAnnotation(HutoolController.class, Api.class); LOGGER.info("annotationUtil api value:{}", api.description()); //Gets the value of the annotation of the specified type Object annotationValue = AnnotationUtil.getAnnotationValue(HutoolController.class, RequestMapping.class);
SecureUtil
Encryption and decryption tool class, which can be used for MD5 encryption.
//MD5 encryption String str = "123456"; String md5Str = SecureUtil.md5(str); LOGGER.info("secureUtil md5:{}", md5Str);
CaptchaUtil verification code tool class, which can be used to generate graphic verification code.
//Generate verification code picture LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100); try { request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode()); response.setContentType("image/png");//Tell the browser that the output is a picture response.setHeader("Pragma", "No-cache");//Disable browser caching response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expire", 0); lineCaptcha.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); }
There are many tools in Hutool. You can refer to the official website: https://www.hutool.cn/
Of course, there are many other very convenient methods. Keep it for yourself! Using Hutool tools can greatly improve your development efficiency!
< END >