生成ID模板:年月日时分秒+6位自增码

由于生成订单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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-26 08:22:55

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

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

class TimeCount { // 临时变量,存放当前类能表示的最大年份值 private static ulong MaxYear = 0; /// <summary> /// 获取毫秒能表示的最大年份数 /// </summary> /// <returns>年份数最大值</returns> public static ulong GetMaxYearCount() { if (TimeCount.MaxYear != 0) return Time

计算一个人的年龄(年月日时分秒),有不对的地方希望大家指出!

想想我们可以做一个计时器,记录一下我们走过了多少时光.看了一下网上别人的一些代码,记录年月的都并不科学,甚至很麻烦,自己倒腾了一上午,总算弄出来了一个. 自己觉得还比较科学,暂时没有发现BUG,如果哪里有错,希望大家指出来! 上代码: <!doctype html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&

年月日时分秒加星期的即时显示的JS

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

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

获取当前年月日(字符串): select CONVERT(varchar(11),GETDATE(),112) 获取当前时间的时分秒(':'隔开): select CONVERT(varchar(12),GETDATE(),108) 将年月日时分秒拼接成一条字符串: select CONVERT(varchar(11),GETDATE(),112)+REPLACE(CONVERT(varchar(12),GETDATE(),108),':','')

关于时间的操作(Java版)——将毫秒转换为年月日时分秒

第一种方式: import java.util.Calendar; import java.util.TimeZone; public class Test { /** * 将毫秒转换为年月日时分秒 * * @author GaoHuanjie */ public String getYearMonthDayHourMinuteSecond(long timeMillis) { Calendar calendar = Calendar.getInstance(TimeZone.getTimeZo

php实现显示网站运行时间-秒转换年月日时分秒

<?php // 设置时区 date_default_timezone_set('Asia/Shanghai'); /** * 秒转时间,格式 年 月 日 时 分 秒 * * @author [email protected] * @param int $time * @return array|boolean */ function Sec2Time($time){ if(is_numeric($time)){ $value = array( "years" => 0,

获取当前时间---年月日时分秒------iOS

方式一:XXXX年-XX月-XX日  XX时:XX分:XX秒的格式 - (IBAction)LoginAction:(UIButton *)sender { NSDate *date = [NSDate date];        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];            [formatter setDateStyle:NSDateFormatterMediumStyle];        [f

jquery 的日期时间控件(年月日时分秒)

<!-- import package --> <script type="text/javascript" src="JS/jquery.js"></script> <script type="text/javascript" src="JS/jquery-ui-1.7.3/ui/jquery-ui-1.7.3.custom.js"></script> <sc

获取年月日时分秒

NSDate *now = [NSDate date]; NSCalendar *cal = [NSCalendar currentCalendar]; unsigned int unitFlags = NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dd = [cal components:unitFlags fromDate: