In the process of system development, we will always encounter the need to define their own tool class, do some data conversion, string operation, date processing, encryption and decryption, decoding, amount calculation and so on, each time to do the system will have to copy these code to copy, time-consuming and laborious not elegant.

Some of you who do Java know this tool, Hutool, I can call it Java’s treasure trove of tools, complete with a POM to introduce dependencies, you don’t need to write your own utility classes anymore, you don’t even need util package in your projects.

Let’s take a look at what treasures Hutool has.

The official website of Hutool is www.hutool.cn/

Contains the following components:

Project POM introduced

<dependency> <groupId>cn. Hutool </groupId> <artifactId>hutool-all</artifactId> <version>5.7.5</version> </dependency>Copy the code

Here are some of my own experiments, post some common code examples, after you try, or very easy to use

String, time, and amount conversion
@test public void testConvert() {/** * String a = "Hello China "; String hex = Convert.toHex(a, CharsetUtil.CHARSET_UTF_8); String raw = Convert.hexToStr(hex, CharsetUtil.CHARSET_UTF_8); System.out.println(raw); */ long b = 1000 * 60 * 60 * 24; long mins = Convert.convertTime(b, TimeUnit.MILLISECONDS, TimeUnit.HOURS); System.out.println(mins); /** * double c = 288.66; System.out.println(Convert.digitToChinese(c)); }Copy the code
Twenty four two HUNDRED and eighty Eight Yuan sixty centsCopy the code
The date of 1
@test public void testDateTime() throws InterruptedException {/** * current time */ Date now = dateutil.date (); System.out.println(now); String nowString = DateUtil.now(); System.out.println(nowString); /** * Current date */ String nowDate = dateutil.today (); System.out.println(nowDate); /** * String retrodate YYYY-MM-DD HH: MM: SS YYYY-MM-DD HH: MM: SS YYYY-MM-DD HH: MM: SS.SSS ** DatePattern */ String dateStr = "2021-2-12"; System.out.println(DateUtil.parse(dateStr)); System.out.println(DateUtil.parse(dateStr, "yyyy-MM-dd")); / / system.out.println (dateutil.year (now)); / / system.out.println (dateutil.year (now)); System.out.println(DateUtil.month(now) + 1); System.out.println(DateUtil.dayOfMonth(now)); System.out.println(DateUtil.hour(now, true)); System.out.println(DateUtil.minute(now)); System.out.println(DateUtil.second(now)); /** * start and end time */ system.out.println (dateutil.beginofDay (now)); System.out.println(DateUtil.endOfDay(now)); /** * time offset */ system.out. println(dateutil.offsethour (now, 3)); System.out.println(DateUtil.yesterday()); }Copy the code
2021-10-26 14:26:12
2021-10-26 14:26:12
2021-10-26
2021-02-12 00:00:00
2021-02-12 00:00:00
2021
10
26
14
26
12
2021-10-26 00:00:00
2021-10-26 23:59:59
2021-10-26 17:26:12
2021-10-25 14:26:13

Copy the code
The date of 2
@test public void testDateTime2() throws InterruptedException {/** * Current time */ Date now = dateutil.date (); System.out.println(now); String nowString = DateUtil.now(); System.out.println(nowString); /** * Date from = dateutil.date (); Date end = DateUtil.offsetMillisecond(from, 988989898); System.out.println(DateUtil.between(from, end, DateUnit.HOUR)); System.out.println(DateUtil.between(from, end, DateUnit.DAY)); Println (" error: "+ dateutil. formatBetween(from, end)); / / system.out. println(" error:" + dateutil. formatBetween(from, end)); /** * TimeInterval timer = dateutil.timer (); Thread.sleep(2000L); System.out.println(timer.interval() + " ms"); timer.intervalRestart(); System.out.println(timer.intervalMinute() + " min"); /** * ChineseDate = new ChineseDate(dateutil.parsedate ("2020-08-28")); / / ChineseDate = new ChineseDate(dateutil.parseDate ("2020-08-28")); String cyclicalYmd = chineseDate.getCyclicalYMD(); System.out.println(cyclicalYmd); /** * Support for JDK 8 LocalDateTime and LocalDate */}Copy the code
2021-10-26 14:26:47 2021-10-26 14:26:47 274 11 difference: 11 days, 10 hours, 43 minutes, 9 seconds 898 millisecondsCopy the code
Finite-state automata – Sensitive word search
@Test public void testDfa() { WordTree tree = new WordTree(); Tree. AddWord (" China "); Tree. AddWord (" mobile "); Tree. AddWord (" hangzhou "); Tree.addword (" mobile "); tree.addword (" mobile "); Tree. AddWord (" hangzhou "); Tree. AddWord (" Chinese "); String text = "中国 mobile, 中国, 中国 mobile, 中国 "; List<String> match = tree.matchAll(text, -1, false, false); System.out.println(match); }Copy the code
[China Mobile, China, China Mobile, Hangzhou, Hangzhou]Copy the code
encryption
@Test public void testEncDec() { String cont = "Hello"; String key = "Yw/Vz4kpJUv0+E/4/LuZEA=="; System.out.println("aes key = " + key); AES aes = SecureUtil.aes(Base64.decode(key)); byte[] encrypt = aes.encrypt(cont); String enc = Base64.encode(encrypt); System.out.println(" ciphertext :" + enc); byte[] decrypt = aes.decrypt(enc); System.out.println(new String(decrypt)); String str = SecureUtil.md5(cont); System.out.println("MD5: " + str); System.out.println("MD5 16 bit: "+ digestutil.md5hex16 (cont)); System.out.println("SHA-256:" + DigestUtil.sha256Hex("Admin@888" + "9348ierj8truigjf03wioekrutgfhj")); // RSA RSA rsa = new RSA(); String privateKey = rsa.getPrivateKeyBase64(); String publicKey = rsa.getPublicKeyBase64(); System.out.println(" privateKey: "+ privateKey); System.out.println(" publicKey: "+ publicKey); String rsaEnc = rsa.encryptBase64(cont, KeyType.PublicKey); System.out.println(" after encryption: "+ rsaEnc); System.out.println(" after decryption: "+ rsa.decryptstr (rsaEnc, keyType.privateKey)); PublicKey publicK = rsa.getPublicKey(); String modulus = HexUtil.encodeHexStr(((RSAPublicKey) publicK).getModulus().toByteArray()); String exponent = HexUtil.encodeHexStr(((RSAPublicKey) publicK).getPublicExponent().toByteArray()); System.out.println(modulus); System.out.println(exponent); HMac mac = new HMac(HmacAlgorithm.HmacSHA256, "password".getBytes()); // b977f4b13f93f549e06140971bded384 String macHex1 = mac.digestHex("123"); System.out.println("HAMC - " + macHex1); // 3DES encryption String content = "test中文"; byte[] desKey = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded(); SymmetricCrypto des = new SymmetricCrypto(SymmetricAlgorithm.DESede, desKey); Byte [] desEnc = des.encrypt(content); // Encrypt byte[] desEnc = des.encrypt(content); Byte [] desDec = des.decrypt(desEnc); byte[] desDec = des.decrypt(desEnc); // Encrypt to hexadecimal String (Hex representation) String encryptHex = des.encrypThex (content); // Decrypts to a String //String decryptStr = des.decryptstr (encryptHex); System.out.println("3DES decryption String: "+ new String(desDec)); }Copy the code
Aes key = Yw/Vz4kpJUv0 + / 4 / E LuZEA = = cipher: xNNdbJKZ/Ml4AWWD8YwO6A = = Hello MD5:8 b1a9953c4611296a827abf8c47804d7 MD5 16: C4611296a827abf8 SHA - 256-01 a83492e88f9a138e5ca0fd3ddb29b233088c70fdf2f1f511a6683b9e725f1d private key: MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAMSTmbFNps1kNWXy/upzcMFvCdiMc6awisow6NZbyuEMs4JvGBt0SvvV51GonnUgPGXHD/BZ 5Yq/V/V9Tq6bvy875gy8xyH5AXuRzboOnkKy2XtEsZOwCiKWeBcigWiyFuV/I7kK6WvAT/CJ5JyQabHzas4vZMKBSOg68cToPW3XAgMBAAECgYBcG50ESxJB YxbyMi3nxTesK089vHHkRQyDOO+goVoqoTzrfQqasp7/6XZ9+LhmwTgLqH57bGP14zpfuNtJTYkC0p+CnPPryfCaLxlIcypwmsHHoUSnYgZHAgybF+dDqHM4 l9rrEXKpu8kj+Y+V7QbLfmooSPtHccrp/SvFXqqsQQJBAOF8iRyTbFOi77NjpdO1z1SwliyEj+uabJxCQsX0Jtghw1+tT7+UNtMSzrW/hKZ3hXBQmfNml/dP chBpHZJ9Km8CQQDfLYwO/aYf2OSjqZuLv7QMvczCcKK9jZ0qO3SesPmPEitVje3xaqfATBtNurkJ0bCv32zdF71BBrw55E02eMcZAkEAkBh+oqVPb69LUS9X 9JBWG3/xqCdataxsxfrOIySggjQpX4Yo1XwJASAeuyTVx2/P9XN8voAMkz3Q3c3e2RNlvwJBAIG1DgaUYp11yARaib1h0DnrgXqsTdkO2f32iiPExXXiYG8b yuOZUh+P2/qEABD13tOIbdPdfS+yw2h+CGYVMdkCQQDIN78dp2gVMnczWddP/Ak+ytSLhvsxeGnVIABw+55kXKyOg9ZolqRuFxv5wnscWQrKH7hzy6ZNmcI/ Q + enTtb4 public key: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEk5mxTabNZDVl8v7qc3DBbwnYjHOmsIrKMOjWW8rhDLOCbxgbdEr71edRqJ51IDxlxw/wWeWKv1f1fU6u After the m78vO + + QF7kc26Dp5Cstl7RLGTsAoilngXIoFoshblfyO5CulrwE YMvMch/wieSckGmx82rOL2TCgUjoOvHE6D1t1wIDAQAB encryption: SF+hCQbOD5feqnE+codoDaODePYJvcvklrOZztBtoE3neNtcjAaCwAF+TzvMoELI5DDmaT2jVa5HObeW7SW2fBQ1f3xN+674z1GeblYXyo0Mr3LZmpFl5d2+ VtpFGkewzBS77ThtMkk34MCq3b3nlA6DG65LC67xnDHHOtVshU8 = decrypted: Hello 00c49399b14da6cd643565f2feea7370c16f09d88c73a6b08aca30e8d65bcae10cb3826f181b744afbd5e751a89e75203c65c70ff059e58abf57f57d 4eae9bbf2f3be60cbcc721f9017b91cdba0e9e42b2d97b44b193b00a22967817228168b216e57f23b90ae96bc04ff089e49c9069b1f36ace2f64c281 48 e83af1c4e83d6dd7 010001 HAMC - c58e5f636d1bfdfbd351d9469c308519fe6695f69c08d5f98198459c86f2a746 3 des declassified series: the test in ChineseCopy the code
file
@Test
	public void testFile() {
		File file = FileUtil.newFile("/apprun/test" + File.separator + "chiweitest-" + RandomUtil.randomString(10) + ".txt");
		FileUtil.touch(file);
	}
Copy the code

Snowflake algorithm -ID generation
@Test public void testId() throws InterruptedException { int machine = getMachinePiece(); int process = getProcessPiece(); System.out.println(machine + "--" + process); System.out.println((machine & 31) + "--" + (process & 31)); Snowflake sf = IdUtil.getSnowflake(); // IdUtil.createSnowflake(machine&31, process&31); Map<Long, String> ids = MapUtil.newConcurrentHashMap(); int counter = 10; int threadNum = 2; TimeInterval timer = DateUtil.timer(); ConcurrencyTester tester = ThreadUtil. ConcurrencyTest (threadNum, () - > {/ / test logic content for (int I = 0; i < counter; i++) { ids.put(sf.nextId(), ""); // String temp = IdUtil.randomUUID(); // ids.add(IdUtil.randomUUID()); }}); // Get the total execution time in milliseconds console.log (tester.getInterval()); System.out.println(timer.interval() + "ms"); system.out.println (timer.interval() +" ms"); System.out.println("SIZE: "+ ids.size()); System.out.println(ids); System.out.println(Long.MAX_VALUE); }Copy the code
738459648--18962 0--18 39 45 MS SIZE: 20 {1452885332411842562=, 1452885332411842563=, 1452885332411842560=, 1452885332411842561=, 1452885332411842566=, 1452885332411842567=, 1452885332411842564=, 1452885332411842565=, 1452885332411842570=, 1452885332411842571=, 1452885332411842568=, 1452885332411842569=, 1452885332411842574=, 1452885332411842575=, 1452885332411842572=, 1452885332411842573 = 1452885332411842578 = 1452885332411842579 = = 1452885332411842576, 1452885332411842577 =} 9223372036854775807Copy the code
Network related
@test public void testNet() {String IP = "192.168.2.65"; long ipLong = 2130706433L; String long2ip = NetUtil.longToIpv4(ipLong); System.out.println(long2ip); long ip2long = NetUtil.ipv4ToLong(ip); System.out.println(ip2long); / / port detection System. The out. Println (" port is connected: "+ NetUtil. IsUsableLocalPort (6379)); System.out.println(" Port valid: "+ netutil.isvalidPort (77777)); // IP desensitization system.out.println (netutil.hideippart (IP)); / / for network information System. The out. Println (NetUtil. GetNetworkInterfaces ()); // eth4 (Realtek PCIe GBE Family Controller) System.out.println(NetUtil.getNetworkInterface("eth4")); System.out.println(NetUtil.LOCAL_IP); System.out.println("============================="); System.out.println(NetUtil.getLocalHostName()); }Copy the code
127.0.0.1 3232236097 Port connectivity: true Port validity: * [name:lo (Software Loopback Interface 1), name:net0 (WAN Miniport (SSTP)), name:net1 (WAN Miniport (L2TP)), name:net2 (WAN Miniport (PPTP)), name:ppp0 (WAN Miniport (PPPOE)), name:eth0 (WAN Miniport (IPv6)), name:eth1 (WAN Miniport (Network Monitor)), name:eth2 (WAN Miniport (IP)), Name: PPP1 (RAS Async Adapter), Name: NET3 (WAN Miniport (IKEv2)), Name :eth3 (Bluetooth device (Personal area network)), Name :net4 (Bluetooth device (RFCOMM TDI)), name:eth4 (Realtek PCIe GBE Family Controller), Name: WLAN0 (Intel(R) Dual Band Wireless-AC 7260), name: NET5 (Bluetooth device (RFCOMM TDI) #2) Name :eth5 (Bluetooth device (Personal area network) #2), name:net6 (Microsoft 6TO4 Adapter), name:eth6 (SecTap Adapter), name:eth7 (Sangfor SSL VPN CS Support System VNIC), name:net7 (Microsoft ISATAP Adapter #7), name:eth8 (TAP-Windows Adapter V9), name:net8 (Microsoft ISATAP Adapter #5), name:eth9 (VirtualBox Host-Only Ethernet Adapter), name:eth10 (Gateway NC Adapter), name:net9 (Microsoft ISATAP Adapter), name:net10 (Microsoft ISATAP Adapter #2), name:net11 (Microsoft ISATAP Adapter #6), name:net12 (Microsoft ISATAP Adapter #8), name:net13 (Microsoft ISATAP Adapter #3), name:net14 (Microsoft ISATAP Adapter #4), name:wlan1 (Microsoft Virtual WiFi Miniport Adapter), name:eth11 (Realtek PCIe GBE Family Controller-VirtualBox NDIS Light-Weight Filter-0000), name:eth12 (Realtek PCIe GBE Family Controller-QoS Packet Scheduler-0000), name:eth13 (Realtek PCIe GBE Family Controller-WFP LightWeight Filter-0000), name:eth14 (Realtek PCIe GBE Family Controller-Npcap Packet Driver (NPCAP)-0000), name:wlan2 (Intel(R) Dual Band Wireless-AC 7260-VirtualBox NDIS Light-Weight Filter-0000), name:wlan3 (Intel(R) Dual Band Wireless-AC 7260-QoS Packet Scheduler-0000), name:eth15 (Realtek PCIe GBE Family Controller-VpnCDrv LightWeight Filter-0000), name:eth16 (SecTap Adapter-VirtualBox NDIS Light-Weight Filter-0000), name:eth17 (SecTap Adapter-QoS Packet Scheduler-0000), name:eth18 (SecTap Adapter-WFP LightWeight Filter-0000), name:wlan4 (Intel(R) Dual Band Wireless-AC 7260-Npcap Packet Driver (NPCAP)-0000), name:eth19 (SecTap Adapter-Npcap Packet Driver (NPCAP)-0000), name:eth20 (WAN Miniport (Network Monitor)-Npcap Packet Driver (NPCAP)-0000), name:eth21 (WAN Miniport (Network Monitor)-VpnCDrv LightWeight Filter-0000), name:eth22 (WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000), name:eth23 (WAN Miniport (IP)-Npcap Packet Driver (NPCAP)-0000), name:eth24 (WAN Miniport (IP)-VpnCDrv LightWeight Filter-0000), name:eth25 (WAN Miniport (IP)-QoS Packet Scheduler-0000), name:eth26 (WAN Miniport (IPv6)-Npcap Packet Driver (NPCAP)-0000), name:eth27 (WAN Miniport (IPv6)-VpnCDrv LightWeight Filter-0000), name:eth28 (WAN Miniport (IPv6)-QoS Packet Scheduler-0000), name:eth29 (Sangfor SSL VPN CS Support System VNIC-VirtualBox NDIS Light-Weight Filter-0000), name:eth30 (Sangfor SSL VPN CS Support System VNIC-QoS Packet Scheduler-0000), name:eth31 (Sangfor SSL VPN CS Support System VNIC-WFP LightWeight Filter-0000), name:eth32 (SecTap Adapter-VpnCDrv LightWeight Filter-0000), name:eth33 (Sangfor SSL VPN CS Support System VNIC-VpnCDrv LightWeight Filter-0000), name:eth34 (TAP-Windows Adapter V9-VirtualBox NDIS Light-Weight Filter-0000), name:eth35 (TAP-Windows Adapter V9-QoS Packet Scheduler-0000), name:eth36 (TAP-Windows Adapter V9-WFP LightWeight Filter-0000), name:wlan5 (Intel(R) Dual Band Wireless-AC 7260-Virtual WiFi Filter Driver-0000), name:eth37 (TAP-Windows Adapter V9-Npcap Packet Driver (NPCAP)-0000), name:eth38 (Gateway NC Adapter-VirtualBox NDIS Light-Weight Filter-0000), name:eth39 (Gateway NC Adapter-QoS Packet Scheduler-0000), name:eth40 (Gateway NC Adapter-WFP LightWeight Filter-0000), name:eth41 (Gateway NC Adapter-Npcap Packet Driver (NPCAP)-0000), name:eth42 (TAP-Windows Adapter V9-VpnCDrv LightWeight Filter-0000), Name :eth43 (Bluetooth device (Personal area network) # 2-NPCAP Packet Driver (Npcap)-0000), name:eth44 (Gateway NC Adapter-VpnCDrv LightWeight Filter-0000), name:wlan6 (Intel(R) Dual Band Wireless-AC 7260-Native WiFi Filter Driver-0000), name:wlan7 (Intel(R) Dual Band Wireless-AC 7260-VpnCDrv LightWeight Filter-0000), name:wlan8 (Intel(R) Dual Band Wireless-AC 7260-WFP LightWeight Filter-0000), Name: PPP2 (RAS Async Adapter-NpCap Packet Driver (Npcap)-0000)] name:eth4 (Realtek PCIe GBE Family Controller) 127.0.0.1  ============================= chiwei-PCCopy the code
The URL associated
@Test
	public void testUrl() {
		System.out.println(URLUtil.toURI("http://www.baidu.com/df/sdf/df").getHost());
	}
Copy the code
www.baidu.com

Copy the code

There are a lot of other, like graphic verification code, image processing, re, ID, desensitization, compression, XML, etc., in short, once used should fall in love with the code, a lot of elegant.

HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP