directory

  • An overview of the
  • Import the project
  • Introduction to Tools
    • FastJsonUtil(FastJson utility class)
    • DateUtil(Time conversion utility class)
    • RegexUtil(Regular utility class)
    • LanguageUtil
    • NetworkUtil(Utility class for getting local IP and hostname)
    • BigDecimalUtil(BigDecimal calculation utility class)
    • HttpUtil(HTTP Request utility class)
    • CoordinateTransformUtil(Coordinate system Transformation utility class)
    • EncodeDecodeUtil(Encoding and decoding tools)
    • ProtobufUtil(provides serialization and antisequence of Protobuf format)
    • DeepCopyUtil(Deep copy utility class)
    • GZIPUtil(compressed and decompressed by the Gzip algorithm)
    • CaptchaUtil(CaptchaUtil utility Class)
    • UrlParamsUtil (utility class for handling URL parameters)
    • EmailUtil(Mail delivery utility class)
  • About open source
  • Contributor
  • Update log

The project address

Making: github.com/LJWLgl/Comm…

An overview of the

This warehouse is a summary of common Java development tool classes, if you are useful welcome to start, in addition, if you are interested in maintaining the warehouse with me, welcome to mention PR or issue, let us have a more powerful tool library and work together.

Import the project

< the dependency > < groupId > IO. Making. LJWLGL < / groupId > < artifactId > common - util < / artifactId > < version > 2.0.7 < / version > </dependency>Copy the code

Introduction to Tools

FastJsonUtil(FastJson utility class)

With the help of FastJson to achieve serialization and anti-sequence, at the same time to achieve their own Json node increase, delete, and DESensitization of Json key fields

  • ToJsonString (Object Object), serialize Json
  • toJsonString(Object object, String… ReAttrs), remove unnecessary attributes when serializing Json transfers
  • IsJSON (String STR) to check whether it is a Json String
  • Eval (String json, String path) to retrieve objects from JSON based on path
  • Eval (String JSON, String path, Class CLZ)
  • List evals(String JSON, String path, Class CLZ), retrieves the result from JSON according to the path and reverses the sequence into JavaBean. This method supports only array
  • Remove (String json, String path) : deletes specified properties according to path
  • Replace (String JSON, String Path, Object Value) : Replaces specified properties based on path. Only JSONObject is supported
  • ReplaceNew (String JSON, String Path, String Value) replaces specified properties according to path. JSONArray is supported, but path expressions are not supported
  • Put (String JSON, String Path, Object obj) : adds specified objects to JSON based on path. Only JSONObject is supported
  • ParseObject (String JSON, Class clazz), deserialize JSON
  • List parseArray(String json, Class clazz), deserialize List
  • String getJsonValue(String jsonStr, String key), obtains the value of a node in the Json String
  • ContainsKey (String jsonStr, String key) checks whether the Json String contains a node
  • containsKey(String jsonStr, String… Keys) to determine whether the Json string contains certain attributes
  • String encryptJson(String JSON, List Paths) encrypts node properties of JSON, desensitizes key fields, and supports JsonArray
  • String encryptToJson(JSONObject preObject, String[] Paths), ditto

DateUtil(Time conversion utility class)

In the daily development process, our process will need to convert Date to String, or String to Date. This utility class can meet our log development needs roughly:

  • GetCurrentDate () to get the current log, including only the date
  • CalcIntervalDays (Date date1, Date date2) : Calculates the number of days between two dates
  • DayOfWeek (Date Date) : Returns the day of the week corresponding to data
  • GetTodayMinutes (), gets the number of minutes today, such as 18:05 today, returns 1805
  • GetIntervalDate (Date time, int days) getIntervalDate(new Date(), -1)
  • DateToShortDateString (Date Date), convert Date to String, output String contains only year, month and day
  • DateToString (Date Date), convert Date to String, output String contains year, month, day, hour, minute, second
  • StringToDate (String dateStr), convert String to Date, default TimeZone 08, timezone.gettimezone (“Asia/Shanghai”)
  • dateToVoString(Date date), the back end will often generate the View layer String according to the incoming Date and pass it to the front end. This method is calculated into the corresponding Chinese, such as:
    • In 1 minute: Just
    • More than 1 minute and within 1 hour: before (1 minute ago)
    • More than 1 hour and within the day: before (1 hour ago)
    • Yesterday: Yesterday + hour minutes (yesterday 08:30)
    • Before yesterday and within the current year: month/day + hours/minutes (January 1 08:30)
    • Other year: x year x month x day + hour minute (08:30, January 1, 2017)

RegexUtil(Regular utility class)

  • IsMobileExact (CharSequence Input) Specifies the mobile phone number
  • IsEmail (CharSequence Input), whether it is an email
  • IsURL (CharSequence Input), whether it is a URL
  • IsIP (CharSequence input, whether it is an IP address
  • IsMatch (String regex, CharSequence Input) whether to match the regular expression
  • GetMatches (String regex, CharSequence Input) to get the portion of the regular expression
  • .

LanguageUtil

Multilanguage scene utility class, you can use the following utility class to determine which locale the character is

  • IsOnlyLetter (String STR), which contains only letters
  • IsLetter (String STR), whether there are letters and Spaces
  • IsChinese (String STR), whether there are only Chinese characters and Spaces
  • IsLetterAndNumber (String STR), containing only letters, digits, and Spaces
  • IsChineseAndNumber (String STR), whether there are only Chinese characters, numbers and Spaces
  • IsKoreanAndNumber (String STR), whether there are only Korean characters, digits, and Spaces
  • IsJapanAndNumber (String STR), whether only Japanese and numbers

NetworkUtil(Utility class for getting local IP and hostname)

This tool class, which is a reference to the internal implementation of the open source project CAT, provides static methods as follows:

  • GetLocalHostAddress (), returns the local IP address
  • GetLocalHostName (), returns the host name
  • GetLocalInetAddress, returns InetAddress

BigDecimalUtil(BigDecimal calculation utility class)

The BigDecimal class is often used in services that work with money, and here are some of its calculations

  • subtract(double x, double y)
  • add(double x, double y)
  • multiply(double x, double y)
  • divide(double x, double y, int scale)
  • RoundUp (double val), approximate processing, rounded up
  • RoundUp (double val, int scale), approximate processing, roundUp, scale retains the number of points
  • roundDown(double val)
  • roundDown(double val, int scale)

HttpUtil(HTTP Request utility class)

It is also common to request other interfaces in a service

  • DoGet (String URL, Map

    queryParam), get request, queryParam is the parameter
    ,>
  • DoPost (String URL, Map

    params, String json), json request body is a JSON String
    ,>
  • . Overloaded method of doPost

CoordinateTransformUtil(Coordinate system Transformation utility class)

Each coordinate system is converted to each other, such as BD09, WGS84, GCJ02

  • ConvertLatLonByCoordinate (String newCoordinateType, String originalCoordinateType, double lat, double lon), Convert the latitude and longitude of the original coordinate system to the latitude and longitude of the new coordinate system, supporting wGS84 to BD09, bd09T to GCJ02D, etc

EncodeDecodeUtil(Encoding and decoding tools)

  • EncodeWithMD5 (String STR) MD5 encryption for the String
  • EncodeWithSHA1 (String STR) encrypts the String with SHA1
  • EncodeWithSHA256 (String STR), sha-256 encryption of String
  • Encode (String algorithm, String STR) : encrypts a String by specifying an algorithm
  • EncodeBase64 (String STR), Base64 encoding of strings
  • DecodeBase64 (String STR), Base64 decodes the String
  • EncodeUrl (String STR), encoding the URL
  • DecodeUrl (String STR) decodes the URL

ProtobufUtil(provides serialization and antisequence of Protobuf format)

  • Byte [] serialize(T obj) : serialize an object
  • Byte [] serializeList(List objList), serializes an array
  • T unSerialize(byte[] data,Class
    clazz), deserialize the object
  • List unSerializeList(byte[] data, Class clazz), deserialize array

DeepCopyUtil(Deep copy utility class)

  • Object depthClone(Object srcObj), a deep copy of a single Object, is implemented through serialization and anti-sequence. Therefore, the corresponding srcObj must implement the Java.io.Serializable interface
  • List listDepthClone(List List), deep copy of multiple objects, srcObj corresponding to the need to implement java.io.Serializable interface

GZIPUtil(compressed and decompressed by the Gzip algorithm)

  • Compress (String STR), which compresses the String to a GZIP byte array
  • Compress (String STR, String encoding), which compresses strings to GZIP byte arrays
  • Uncompress (byte[] bytes), GZIP decompresses

CaptchaUtil(CaptchaUtil utility Class)

  • String genCaptcha(int count) : generates a random verification code with the specified bit count
  • BufferedImage genCaptchaImg(String captCHA), generates an image for a captcha

UrlParamsUtil (utility class for handling URL parameters)

  • String join(Map

    Map, String separator) converts the Map to String. Separator can be specified. It is used to concatenate the parameters following the URL
    ,>
  • Map

    split(String paramsPath, String separator), parse ulR argument to Map
    ,>
  • Map

    split(String paramsPath) ulR: Map; separator: “=”
    ,>
  • Map

    build(String … KeyValues), convert keyValues to Map
    ,>
  • add(Map

    originMap, String … KeyValues), add keyValues to the original Map
    ,>

EmailUtil(Mail delivery utility class)

Mail tool class is achieved through JavaEmail, enterprise projects will generally specialized services to send mail, but if their own small Demo, using the tool class to send Email is not impossible. Sample Demo below, interested can fork code to explore, code has detailed comments.

public class EmailUtilTest {
    @Before
    public void before(a) throws GeneralSecurityException {
        // If the sender email address is configured, the authorization password can be obtained from the mail carrier
        EmailUtil.config(EmailUtil.SMTP_QQ(false), "[email protected]"."xxxxxx");
    }

    @Test
    public void testSendText(a) throws MessagingException {
        System.out.println("test"); }}Copy the code

About open source

This project is an open source project, if there are excerpts from this project code, please indicate the source! (Code word is not easy, please respect open source spirit)

Update log

  • August 2018, First Commit
  • LanguageUtil & released version 2.0.7 on 22 May 2020
  • EncodeDecodeUtil, CaptchaUtil & release 2.0.8 on 24 May 2020 (under review)