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 to generate serial number in batch

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

Share

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

This article mainly introduces the relevant knowledge of "how to use Java to generate serial numbers in batches". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope that this article "how to use Java to generate serial numbers in batches" can help you solve the problem.

Preface

The generation of serial number can be said to be a relatively common demand in enterprises, especially in order business.

Generally speaking, it is necessary to ensure the uniqueness of the serial number.

If there are no restrictions on length and characters, you can use UUID directly to generate a unique string, or you can directly use the primary key in the database table, which is unique.

So, if you limit how many digits a serial number must have, how can this be generated?

You can use the "prefix + date + number" method (ps: this method requires caching)

Prefix: in order to better identify which type this serial number belongs to

Date: to prevent repetition

Number: to indicate the serial number where the current pipeline is located.

Requirements: generate a unique 17-digit serial number, "LSH" + yyyyMMdd+6 digits

The code implements import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.concurrent.atomic.AtomicInteger;public class SerialNoTest {public static void main (String [] args) {String serialNo = generateSerialNo (); System.out.println ("generated serial number:" + serialNo) } / * * generate 17-digit unique serial number, "LSH" + yyyyMMdd+6 digit * 6 digits, such as: 000001 * @ return * / private static String generateSerialNo () {/ / define the serial number String serialNo = null to be returned / / query today's date first, format: "yyyyMMdd" String todayDate = new SimpleDateFormat ("yyyyMMdd") .format (new Date ()); / / fixed letter prefix spliced today's date to form a new complete prefix, that is, cached key String cacheKey = "LSH" + todayDate / / whether the num data is cached through the key query. The cache operation encapsulates the tool class Long codeNum = cacheService.getCache (cacheKey, Long.class) according to its own project; / / if the cache query has a value, the value is + 1, and then the value is assigned to the next serial number if (null! = codeNum) {codeNum = codeNum + 1L } else {/ / if the cache query has no value, directly assign it to 1 codeNum = 1L;} / / serial number = cache key + concatenated value = prefix + date + concatenated value serialNo = getCodeOfSix (cacheKey, codeNum.intValue ()) / / set the cache. Calling this method will automatically save the value+1 corresponding to key: cacheService.incr (cacheKey, getSeconds ()) for the rest of the day; return serialNo } / * concatenate the values into the corresponding number of digits * @ param prefix prefix: "LSH" + yyyyMMdd * @ param nowNum number to be generated * @ return spliced serial number * / public static String getCodeOfSix (String prefix,int nowNum) {/ / code StringBuilder codeSb = new StringBuilder () to be returned / / the number that needs to be concatenated is StringBuilder numSb = new StringBuilder (); / / the encapsulated digital object, in which the volatile keyword is added to ensure thread safety AtomicInteger count = new AtomicInteger (nowNum); / / complement the value to the 6-bit string if (count.get ())

< 10) { numSb.append("00000").append(count.get()); } else if(count.get() < 100){ numSb.append("0000").append(count.get()); }else if(count.get() < 1000){ numSb.append("000").append(count.get()); }else if(count.get() < 10000){ numSb.append("00").append(count.get()); }else if(count.get() < 100000){ numSb.append("0").append(count.get()); } else if (count.get() >

= 100000) {numSb.append (count.get ());} / / concatenate the prefix codeSb.append (prefix) first; / / then concatenate the digits codeSb.append (numSb); return codeSb.toString () } / * get the number of seconds left at the end of the day * @ return * / public static int getSeconds () {/ / get the current time today Calendar curDate = Calendar.getInstance () / / get the date of 0am tomorrow Calendar tommorowDate = new GregorianCalendar (curDate.get (Calendar.YEAR), curDate.get (Calendar.MONTH), curDate.get (Calendar.DATE) + 1,0,0,0) / / returns the difference between 0am tomorrow morning and the current time today (in seconds) return (int) (tommorowDate.getTimeInMillis ()-curDate .getTimeInMillis ()) / 1000;}}

If today is April 22nd, 2021, run the project, the first serial number generated is: LSH20210422000001

The second serial number is: LSH20210422000002, and so on.

It is important to note that:

If the number of digits is limited, 6 digits can generate up to 10w serial numbers per day, so this digit can be adjusted according to the specific volume of business.

If the amount of generation per day is less than 1 week, use 4 digits.

This is the end of the content about "how to use Java to generate serial numbers in batches". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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