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 generate numbers automatically by java

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

Share

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

This article mainly introduces how to automatically generate the number of java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The details are as follows

/ * automatically generate numbering format: yyMM+ four-digit serial number * / @ RequestMapping (params = "createCode") @ ResponseBody public AjaxJson createCode (HttpServletRequest request, String tableName, String fieldName) {AjaxJson j = new AjaxJson (); String sql = "select Max (a." + fieldName + ") max_code from" + tableName + "a"; List list = new ArrayList (); list = jdbcTemplate.queryForList (sql) / / System.out.println ("maximum number:" + list.get (0). Get ("max_comment_code")); String comment_code = ""; String max_code = ""; if (list.size () > 0) {max_code = (String) list.get (0). Get ("max_code");} SimpleDateFormat format = new SimpleDateFormat ("yyMM"); / / time string generation method String uid_pfix = format.format (new Date ()) / / combine the first part of the serial number, the time string, such as: 1601 System.out.println ("time=" + format.format (new Date (); if (max_code! = null & & max_code.contains (uid_pfix)) {String uid_end= max_code.substring (4,8); / / intercept the last four digits of the string. Result: 0001 / / System.out.println ("uid_end=" + uid_end) Int endNum= Integer.parseInt (uid_end); / / convert 0001 of String type to 1 / / System.out.println of int type ("endNum=" + endNum); int tmpNum= 10000 + endNum + 1; / / result 10002 / / System.out.println ("tmpNum=" + tmpNum); comment_code = uid_pfix + UtilMethod.subStr ("" + tmpNum, 1) / remove the first 1 of 10002 and spell it into a 1601260002 string} else {comment_code = uid_pfix + "0001";} / / System.out.println (comment_code); Map map = new HashMap (); map.put ("msg", comment_code); j.setAttributes (map); return j } / / Public method public class UtilMethod {/ * * how to remove the first 1 of 10002: * @ param str * @ param start * @ return * / public static String subStr (String str, int start) {if (str = = null | | str.equals (") | | str.length () = = 0) return"; if (start < str.length ()) {return str.substring (start) } else {return ";}

Front end:

/ / automatically generate numbers; format: yyMM0001function createCode () {var id = $("[name='id']") .val (); / / alert (id.length); if (id.length)

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