因为生成订单ID、商品ID 或者什么什么ID的,不想用自增,又怕反复,于是就用  年与日时分秒 + 6位自增码 (共计20位长度)来当作ID

注意:假设你的ID是Long型。就要注意,Long的最大长度为19位,假设直接转的话会有问题,建议改为年月日时分秒+5位随机数

详细代码:

private static int sequence = 0;

	private static int length = 6;

	/**
* YYYYMMDDHHMMSS+6位自增长码(20位)
* @author shijing
* 2015年6月29日下午1:25:23
* @return
*/
public static synchronized String getLocalTrmSeqNum() {
sequence = sequence >= 999999 ? 1 : sequence + 1;
String datetime = new SimpleDateFormat("yyyyMMddHHmmss")
.format(new Date());
String s = Integer.toString(sequence);
return datetime +addLeftZero(s, length);
} /**
* 左填0
* @author shijing
* 2015年6月29日下午1:24:32
* @param s
* @param length
* @return
*/
public static String addLeftZero(String s, int length) {
// StringBuilder sb=new StringBuilder();
int old = s.length();
if (length > old) {
char[] c = new char[length];
char[] x = s.toCharArray();
if (x.length > length) {
throw new IllegalArgumentException(
"Numeric value is larger than intended length: " + s
+ " LEN " + length);
}
int lim = c.length - x.length;
for (int i = 0; i < lim; i++) {
c[i] = '0';
}
System.arraycopy(x, 0, c, lim, x.length);
return new String(c);
}
return s.substring(0, length); }

以下是測试的结果:

2015070816503700001

生成ID模板:年月日时分秒+6位自增码的更多相关文章

  1. C# 版本的 计时器类:精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出

    class TimeCount { // 临时变量,存放当前类能表示的最大年份值 ; /// <summary> /// 获取毫秒能表示的最大年份数 /// </summary> ...

  2. Sql 中获取年月日时分秒的函数

    getdate():获取系统当前时间 dateadd(datepart,number,date):计算在一个时间的基础上增加一个时间后的新时间值,比如:dateadd(yy,30,getdate()) ...

  3. 年月日时分秒毫秒+随机数getSerialNum

    package com.creditharmony.apporveadapter.core.utils; import java.io.ByteArrayInputStream; import jav ...

  4. js获取当前时间的年月日时分秒以及时间的格式化

    1.获取当前时间 var myDate = new Date(); 2.获取时间中的年月日时分秒 myDate.getYear(); // 获取当前年份(2位) myDate.getFullYear( ...

  5. nginx日志修改时间格式为年月日时分秒

    先解除这段注释,使用自定义日志格式 $time_iso8601 生成格式:--20T09::+: $time_local 生成格式: /Apr/::: + 还是选择年月日时分秒看起来舒服一点

  6. Swift3.0 iOS获取当前时间 - 年月日时分秒星期

    Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...

  7. H面试程序(1)编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的 下一秒

    编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒. 如输入 2004 年 12 月 31 日 23 时 59 分 59 秒,则输出 2005年 1 月 1 日 0 时 0 分 0 秒. ...

  8. [置顶] java得到前一个月的年月日时分秒

    import java.util.Calendar; /** * 得到前一个月的年月日时分秒 * @author Mr.hu * 2013-6-28上午12:00:35 * Class Explain ...

  9. sql server获取当前年月日 时分秒

    获取当前年月日(字符串): ),) 获取当前时间的时分秒(':'隔开): ),) 将年月日时分秒拼接成一条字符串: ),)),),':','')

随机推荐

  1. eclipse 性能调优之内存分配

    转自:http://blog.csdn.net/defonds/article/details/6289236 如果觉得自己的 eclipse 比较慢,可以通过修改 %eclipse_home%/ec ...

  2. [Java基础] Java对象内存结构

    转载地址:http://www.importnew.com/1305.html 原文于2008年11月13日 发表, 2008年12月18日更新:这里还有一篇关于Java的Sizeof运算符的实用库的 ...

  3. 玩转Google开源C++单元测试框架Google Test系列(gtest)(转)

    转自:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Googl ...

  4. saltstack之crontab管理用法

    一.创建定时任务 crontab: #脚本脚识 cron.present: #模板:cron 计划任务 功能:present - name: /usr/sbin/ntpdate times.aliyu ...

  5. IDEA java开发 Restful 风格的WebService

    官网:https://www.jetbrains.com/help/idea/restful-webservices.html 1.在IntelliJ中创建新项目,选择Java Enterprise ...

  6. POJ 1511 链式前向星+SPFA

    #include<iostream> #include<cstdio> #include<cstdlib> using namespace std; const i ...

  7. Spring框架学习(4)spring整合hibernate

    内容源自:spring整合hibernate    spring整合注解形式的hibernate 这里和上一部分学习一样用了模板模式, 将hibernate开发流程封装在ORM层提供的模板类Hiber ...

  8. C语言面试问题

    内容源自:C语言面试题大汇总 P.S.只摘取了自己觉得可能会被问到的以及不会的. static有什么用途?(请至少说明两种) 1.限制变量的作用域2.设置变量的存储域 引用与指针有什么区别? 1) 引 ...

  9. 给ubuntu设置静态ip —— How to set static IP Address in Ubuntu Server 16.04

    原文: http://www.configserverfirewall.com/ubuntu-linux/ubuntu-set-static-ip-address/ ----------------- ...

  10. java 过滤器补充

    多个Filter按照在配置文件中配置的filter顺序执行. 在web.xml文件中配置该Filter,使用init-param元素为该Filter配置参数,init-param可接受如下两个子元素: ...