1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import
java.text.DecimalFormat;
import
java.text.SimpleDateFormat;
import
java.util.Date;
public
class
PrimaryGenerater {
private
static
final
String SERIAL_NUMBER =
"XXXX"
;
// 流水号格式
private
static
PrimaryGenerater primaryGenerater =
null
;
private
PrimaryGenerater() {
}
/**
* 取得PrimaryGenerater的单例实现
*
* @return
*/
public
static
PrimaryGenerater getInstance() {
if
(primaryGenerater ==
null
) {
synchronized
(PrimaryGenerater.
class
) {
if
(primaryGenerater ==
null
) {
primaryGenerater =
new
PrimaryGenerater();
}
}
}
return
primaryGenerater;
}
/**
* 生成下一个编号
*/
public
synchronized
String generaterNextNumber(String sno) {
String id =
null
;
Date date =
new
Date();
SimpleDateFormat formatter =
new
SimpleDateFormat(
"yyyyMMdd"
);
if
(sno ==
null
) {
id = formatter.format(date) +
"0001"
;
}
else
{
int
count = SERIAL_NUMBER.length();
StringBuilder sb =
new
StringBuilder();
for
(
int
i =
0
; i < count; i++) {
sb.append(
"0"
);
}
DecimalFormat df =
new
DecimalFormat(
"0000"
);
id = formatter.format(date)
+ df.format(
1
+ Integer.parseInt(sno.substring(
8
,
12
)));
}
return
id;
}
}
|
本文转自yunlielai51CTO博客,原文链接:http://blog.51cto.com/4925054/1333368,如需转载请自行联系原作者