Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JAVA Treasure tool hutool

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use JAVA treasure tool hutool. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

Contains the following components:

Introduction of engineering pom

Cn.hutool hutool-all 5.7.5

Here are some of my own experiments, paste some commonly used code examples, later you try, it is still very easy to use

String, time, amount conversion

@ Test public void testConvert () {/ * string Encoding conversion * / 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); / * time conversion, millisecond to XX hours, etc. / long b = 1000 * 60 * 60 * 24 Long mins = Convert.convertTime (b, TimeUnit.MILLISECONDS, TimeUnit.HOURS); System.out.println (mins); / * * amount case conversion * / double c = 288.66; System.out.println (Convert.digitToChinese (c));}

Hello China

twenty-four

Two hundred and twenty-one hundred and twenty-eight yuan land angle land points

Date 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 date yyyy-MM-dd HH:mm:ss yyyy-MM-dd HH:mm:ss yyyy-MM-dd HH:mm 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")) / * get some part of the Date object * / 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 ());}

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

ten

twenty-six

fourteen

twenty-six

twelve

2021-10-26 00:00:00

2021-10-26 23:59:59

2021-10-26 17:26:12

2021-10-25 14:26:13

Date 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); / * * time difference * / 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)); / * * format time difference, accurate to millisecond gap by default * / System.out.println ("difference:" + DateUtil.formatBetween (from, end)); / * * timer * / TimeInterval timer = DateUtil.timer (); Thread.sleep (2000L); System.out.println (timer.interval () + "ms"); timer.intervalRestart () System.out.println (timer.intervalMinute () + "min"); / * * format string DatePattern * / / * Lunar calendar * / ChineseDate chineseDate = new ChineseDate (DateUtil.parseDate ("2020-08-28")); String cyclicalYmd = chineseDate.getCyclicalYMD (); System.out.println (cyclicalYmd); / * * support for JDK 8 LocalDateTime and LocalDate * /}

2021-10-26 14:26:47

2021-10-26 14:26:47

two hundred and seventy four

eleven

Difference: 11 days 10 hours 43 minutes 9 seconds 898 milliseconds

Finite state automaton-sensitive word search

@ Test public void testDfa () {WordTree tree = new WordTree (); tree.addWord ("China"); tree.addWord ("Mobile"); tree.addWord ("Hangzhou"); tree.addWord ("Mobile Communications"); tree.addWord ("Hangzhou"); tree.addWord ("Chinese"); String text = "China Mobile in China, all Chinese, Mobile Hangyan in Hangzhou, Hangzhou" List match = tree.matchAll (text,-1, false, false); System.out.println (match);}

[China, Mobile, China, China Mobile, Hangzhou, Hangzhou]

Encryption and decryption

@ 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 ("Private key:" + privateKey) System.out.println ("public key:" + publicKey); String rsaEnc = rsa.encryptBase64 (cont, KeyType.PublicKey); System.out.println ("encrypted:" + rsaEnc); System.out.println ("decrypted:" + 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 Chinese"; byte [] desKey = SecureUtil.generateKey (SymmetricAlgorithm.DESede.getValue ()). GetEncoded (); SymmetricCrypto des = new SymmetricCrypto (SymmetricAlgorithm.DESede, desKey) / / encrypt byte [] desEnc = des.encrypt (content); / / decrypt byte [] desDec = des.decrypt (desEnc); / / encrypt to hexadecimal string (represented by Hex) String encryptHex = des.encryptHex (content); / / decrypt to string / / String decryptStr = des.decryptStr (encryptHex); System.out.println ("3DES decryption string:" + new String (desDec));}

Aes key = Yw/Vz4kpJUv0+E/4/LuZEA==

Ciphertext: xNNdbJKZ/Ml4AWWD8YwO6A==

Hello

MD5: 8b1a9953c4611296a827abf8c47804d7

MD5 16-bit: c4611296a827abf8

SHA-256:01a83492e88f9a138e5ca0fd3ddb29b233088c70fdf2f1f511a6683b9e725f1d

Private key: MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAMSTmbFNps1kNWXy/upzcMFvCdiMc6awisow6NZbyuEMs4JvGBt0SvvV51GonnUgPGXHD/BZ5Yq/V/V9Tq6bvy875gy8xyH5AXuRzboOnkKy2XtEsZOwCiKWeBcigWiyFuV/I7kK6WvAT/CJ5JyQabHzas4vZMKBSOg68cToPW3XAgMBAAECgYBcG50ESxJBYxbyMi3nxTesK089vHHkRQyDOO+goVoqoTzrfQqasp7/6XZ9+LhmwTgLqH57bGP14zpfuNtJTYkC0p+CnPPryfCaLxlIcypwmsHHoUSnYgZHAgybF+dDqHM4l9rrEXKpu8kj+Y+V7QbLfmooSPtHccrp/SvFXqqsQQJBAOF8iRyTbFOi77NjpdO1z1SwliyEj+uabJxCQsX0Jtghw1+tT7+UNtMSzrW/hKZ3hXBQmfNml/dPchBpHZJ9Km8CQQDfLYwO/aYf2OSjqZuLv7QMvczCcKK9jZ0qO3SesPmPEitVje3xaqfATBtNurkJ0bCv32zdF71BBrw55E02eMcZAkEAkBh+oqVPb69LUS9X9JBWG3/xqCdataxsxfrOIySggjQpX4Yo1XwJASAeuyTVx2/P9XN8voAMkz3Q3c3e2RNlvwJBAIG1DgaUYp11yARaib1h0DnrgXqsTdkO2f32iiPExXXiYG8byuOZUh+P2/qEABD13tOIbdPdfS+yw2h+CGYVMdkCQQDIN78dp2gVMnczWddP/Ak+ytSLhvsxeGnVIABw+55kXKyOg9ZolqRuFxv5wnscWQrKH7hzy6ZNmcI/q+enTtb4

Public key: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEk5mxTabNZDVl8v7qc3DBbwnYjHOmsIrKMOjWW8rhDLOCbxgbdEr71edRqJ51IDxlxw/wWeWKv1f1fU6um78vO+YMvMch+QF7kc26Dp5Cstl7RLGTsAoilngXIoFoshblfyO5CulrwE/wieSckGmx82rOL2TCgUjoOvHE6D1t1wIDAQAB

After encryption: SF+hCQbOD5feqnE+codoDaODePYJvcvklrOZztBtoE3neNtcjAaCwAF+TzvMoELI5DDmaT2jVa5HObeW7SW2fBQ1f3xN+674z1GeblYXyo0Mr3LZmpFl5d2+VtpFGkewzBS77ThtMkk34MCq3b3nlA6DG65LC67xnDHHOtVshU8=

After decryption: Hello

00c49399b14da6cd643565f2feea7370c16f09d88c73a6b08aca30e8d65bcae10cb3826f181b744afbd5e751a89e75203c65c70ff059e58abf57f57d4eae9bbf2f3be60cbcc721f9017b91cdba0e9e42b2d97b44b193b00a22967817228168b216e57f23b90ae96bc04ff089e49c9069b1f36ace2f64c28148e83af1c4e83d6dd7

010001

HAMC-c58e5f636d1bfdfbd351d9469c308519fe6695f69c08d5f98198459c86f2a746

3DES decryption string: test Chinese

File

@ Test public void testFile () {File file = FileUtil.newFile ("/ apprun/test" + File.separator + "chiweitest-" + RandomUtil.randomString (10) + ".txt"); FileUtil.touch (file);}

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 ids = MapUtil.newConcurrentHashMap (); int counter = 10; int threadNum = 2; TimeInterval timer = DateUtil.timer () ConcurrencyTester tester = ThreadUtil.concurrencyTest (threadNum, ()-> {/ / logical content of the test for (int I = 0; I < counter; iTunes +) {ids.put (sf.nextId (), "); / / String temp = IdUtil.randomUUID (); / / ids.add (IdUtil.randomUUID ());}}); / / get the total execution time in milliseconds Console.log (tester.getInterval ()) / / get the total execution time in milliseconds System.out.println (timer.interval () + "ms"); System.out.println ("SIZE:" + ids.size ()); System.out.println (ids); System.out.println (Long.MAX_VALUE);}

738459648 Muhami 18962

0Murray 18

thirty-nine

45 ms

SIZE:20

{1452885332411842562belt, 14528853324118425611842563, 14528853324118425611842561, 1452885332411842561, 1452885332411842567, 1452885332411842564, 1452885332411842570, 1452885332411842571, 1452885332411842568, 1452885332411842569, 1452885333311842574, 14528833331184257414528283333118254254255285283333118425741452833332424242424425425425528528333324242424418425742852833332424242441842561185283333242424244254254255285283333242424241842561,14528853333118425661,14528853333118425611842566114528853333118425741452885333311842542542552852833331182542542552852833331184257428528333311842574, 14528853324118425661, 1452885333311842564, 14528853333118425442542552852833332411842574255285283333242418425442561145288533331184256414528853333118425641452885333311842564145288533331184257428528332424242424241842574255285283333242424242418425425528528333324

9223372036854775807

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 probe System.out.println ("Port Connectivity:" + NetUtil.isUsableLocalPort (6379)); System.out.println ("Port valid:" + NetUtil.isValidPort (77777)) / / IP desensitized System.out.println (NetUtil.hideIpPart (ip)); / / get Nic information System.out.println (NetUtil.getNetworkInterfaces ()); / / eth5 (Realtek PCIe GBE Family Controller) System.out.println (NetUtil.getNetworkInterface ("eth5")); System.out.println (NetUtil.LOCAL_IP); System.out.println ("="); System.out.println (NetUtil.getLocalHostName ());}

127.0.0.1

3232236097

Whether the port is connected: true

Whether the port is valid: false

192.168.2.*

[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:eth2 (WAN Miniport (Network Monitor)), name:eth3 (WAN Miniport (IP), name:ppp1 (RAS Async Adapter), name:net3 (WAN Miniport (IKEv2)), name:eth4 (Bluetooth device (personal area network)) Name:net4 (Bluetooth device (RFCOMM Protocol TDI)), name:eth5 (Realtek PCIe GBE Family Controller), name:wlan0 (Intel (R) Dual Band Wireless-AC 7260), name:net5 (Bluetooth device (RFCOMM Protocol TDI) # 2), name:eth6 (Bluetooth device (personal area Network) # 2), name:net6 (Microsoft 6to4 Adapter), name:eth7 (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:eth20 (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:eth21 (Realtek PCIe GBE Family Controller-VirtualBox NDIS Light-Weight Filter-0000) Name:eth22 (Realtek PCIe GBE Family Controller-QoS Packet Scheduler-0000), name:eth23 (Realtek PCIe GBE Family Controller-WFP LightWeight Filter-0000), name:eth24 (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:eth25 (Realtek PCIe GBE Family Controller-VpnCDrv LightWeight Filter-0000) Name:eth26 (SecTap Adapter-VirtualBox NDIS Light-Weight Filter-0000), name:eth27 (SecTap Adapter-QoS Packet Scheduler-0000), name:eth28 (SecTap Adapter-WFP LightWeight Filter-0000), name:wlan4 (Intel (R) Dual Band Wireless-AC 7260-Npcap Packet Driver (NPCAP)-0000), name:eth29 (SecTap Adapter-Npcap Packet Driver (NPCAP)-0000), name:eth30 (WAN Miniport (Network Monitor)-Npcap Packet Driver (NPCAP)-0000), name:eth31 (WAN Miniport (Network Monitor)-VpnCDrv LightWeight Filter-0000) Name:eth32 (WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000), name:eth33 (WAN Miniport (IP)-Npcap Packet Driver (NPCAP)-0000), name:eth34 (WAN Miniport (IP)-VpnCDrv LightWeight Filter-0000), name:eth35 (WAN Miniport (IP)-QoS Packet Scheduler-0000), name:eth36 (WAN Miniport (IPv6)-Npcap Packet Driver (NPCAP)-0000), name:eth37 (WAN Miniport (IPv6)-VpnCDrv LightWeight Filter-0000), name:eth38 (WAN Miniport (IPv6)-QoS Packet Scheduler-0000) Name:eth39 (Sangfor SSL VPN CS Support System VNIC-VirtualBox NDIS Light-Weight Filter-0000), name:eth40 (Sangfor SSL VPN CS Support System VNIC-QoS Packet Scheduler-0000), name:eth41 (Sangfor SSL VPN CS Support System VNIC-WFP LightWeight Filter-0000), name:eth42 (SecTap Adapter-VpnCDrv LightWeight Filter-0000), name:eth43 (Sangfor SSL VPN CS Support System VNIC-VpnCDrv LightWeight Filter-0000), name:eth44 (TAP-Windows Adapter V9-VirtualBox NDIS Light-Weight Filter-0000), name:eth45 (TAP-Windows Adapter V9-QoS Packet Scheduler-0000) Name:eth46 (TAP-Windows Adapter V9-WFP LightWeight Filter-0000), name:wlan5 (Intel (R) Dual Band Wireless-AC 7260-Virtual WiFi Filter Driver-0000), name:eth47 (TAP-Windows Adapter V9-Npcap Packet Driver (NPCAP)-0000), name:eth48 (Gateway NC Adapter-VirtualBox NDIS Light-Weight Filter-0000), name:eth49 (Gateway NC Adapter-QoS Packet Scheduler-0000), name:eth50 (Gateway NC Adapter-WFP LightWeight Filter-0000), name:eth51 (Gateway NC Adapter-Npcap Packet Driver (NPCAP)-0000) Name:eth52 (TAP-Windows Adapter V9-VpnCDrv LightWeight Filter-0000), name:eth53 (Bluetooth device (personal area Network) # 2-Npcap Packet Driver (NPCAP)-0000), name:eth54 (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:eth5 (Realtek PCIe GBE Family Controller)

127.0.0.1

= =

Chiwei-PC

URL correlation

@ Test public void testUrl () {System.out.println (URLUtil.toURI ("/ / www.yisu.com/df/sdf/df"). GetHost ();} above is all the content of the article "how to use hutool, a JAVA Treasure tool". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report