private
String fmtLong(Long val,
int
size){
StringBuilder sb =
new
StringBuilder(
""
);
sb.append(val);
if
(sb.length() < size){
int
cnt = size - sb.length();
for
(
int
i = 0; i < cnt; i++){
sb.insert(0,
"0"
);
}
return
sb.toString();
}
else
if
(sb.length() > size){
return
sb.substring(sb.length() - size, size);
}
else
{
return
sb.toString();
}
}
//
生成
4
位流水号:
0001
——
XXXX
for
(
int
i=1;i<=Integer.
parseInt
(boxCount);i++){
SCMPackInfo packInfo =
new
SCMPackInfo();
String boxNumber=deliverNoticeCode+fmtLong(Long.
valueOf
(i),4);
packInfo.setBoxNumber(boxNumber);
packInfo.setBoxAmount(1.0);
packInfo.setDeliverNotice(deliverNotice);
this
.
entityManager
.save(packInfo);
}
private
String fmtLong(Long val,
int
size){
StringBuilder sb =
new
StringBuilder(
""
);
sb.append(val);
if
(sb.length() < size){
int
cnt = size - sb.length();
for
(
int
i = 0; i < cnt; i++){
sb.insert(0,
"0"
);
}
return
sb.toString();
}
else
if
(sb.length() > size){
return
sb.substring(sb.length() - size, size);
}
else
{
return
sb.toString();
}
}
//
生成
4
位流水号:
0001
——
XXXX
for
(
int
i=1;i<=Integer.
parseInt
(boxCount);i++){
SCMPackInfo packInfo =
new
SCMPackInfo();
String boxNumber=deliverNoticeCode+fmtLong(Long.
valueOf
(i),4);
packInfo.setBoxNumber(boxNumber);
packInfo.setBoxAmount(1.0);
packInfo.setDeliverNotice(deliverNotice);
this
.
entityManager
.save(packInfo);
}
private
String fmtLong(Long val,
int
size){
StringBuilder sb =
new
StringBuilder(
""
);
sb.append(val);
if
(sb.length() < size){
int
cnt = size - sb.length();
for
(
int
i = 0; i < cnt; i++){
sb.insert(0,
"0"
);
}
return
sb.toString();
}
else
if
(sb.length() > size){
return
sb.substring(sb.length() - size, size);
}
else
{
return
sb.toString();
}
}
//
生成
4
位流水号:
0001
——
XXXX
for
(
int
i=1;i<=Integer.
parseInt
(boxCount);i++){
SCMPackInfo packInfo =
new
SCMPackInfo();
String boxNumber=deliverNoticeCode+fmtLong(Long.
valueOf
(i),4);
packInfo.setBoxNumber(boxNumber);
packInfo.setBoxAmount(1.0);
packInfo.setDeliverNotice(deliverNotice);
this
.
entityManager
.save(packInfo);
}
private String fmtLong(Long val, int size){
StringBuilder sb = new StringBuilder("");
sb.append(val);
if (sb.length() < size){
int cnt = size - sb.length();
for (int i = 0; i < cnt; i++){
sb.insert(0, "0");
}
return sb.toString();
}else if (sb.length() > size){
return sb.substring(sb.length() - size, size);
}else{
return sb.toString();
}
}
//生成4位流水号:0001——XXXX
for(int i=1;i<=Integer.parseInt(boxCount);i++){
SCMPackInfo packInfo = new SCMPackInfo();
String boxNumber=deliverNoticeCode+fmtLong(Long.valueOf(i),4);
packInfo.setBoxNumber(boxNumber);
packInfo.setBoxAmount(1.0);
packInfo.setDeliverNotice(deliverNotice);
this.entityManager.save(packInfo);
}
短网址服务就是把长的URL地址转换成短的URL。如google提供的http://goo.gl/
短网址 http://goo.gl/mwsQP 对应的网址就是 http://www.cnblogs.com/zjfree
短网址参考:http://baike.baidu.com/view/2693499.htm
那为什么URL那么多怎么就可以使用5位字符就表示呢? 注意短网址后面的5位字符是区分大小写的。
如果五个字符是(0-9)、(A-Z)、(a-z),那5位字符可以穷举多个呢?62^5 = 916132832个 如果6位呢?62^6 = 56800235584个 500多亿个网址。
这实际上是一个62进制的数字!那么在现实的应用中经常有缩短业务单号的需求,为什么不使用这种办法呢?为了方便输入我们不区分大小写。
我们使用(0-9)、(A-Z) 这样就得到了一个36进制的数字。它能表示多少信息呢?
进制说明
10进制是 0-9 (0123456789)
16进制是 0-F (0123456789ABCDEF)
36进制是 0-F (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ)
信息量计算 多少位就是36的多少次方。
1位时 36^1 = 36
2位时 36^2 = 1296
3位时 36^3 = 46656
4位时 36^4 = 1679616
5位时 36^5 = 60466176
6位时 36^6 = 2176782336
7位时 36^7 = 78364164096
10进制和36进制互转代码
/// <summary>
/// Convert36 的摘要说明
/// </summary>
public
class
Convert36
{
public
Convert36()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private
const
string
BASE_CHAR =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
;
/// <summary>
/// 转换为段字符
/// </summary>
/// <param name="num"></param>
/// <param name="length"></param>
/// <returns></returns>
public
static
string
GetShortNo(
int
num,
int
length)
{
string
str =
""
;
while
(num > 0)
{
int
cur = num % BASE_CHAR.Length;
str = BASE_CHAR[cur] + str;
num = num / BASE_CHAR.Length;
}
if
(str.Length > length)
{
str = str.Substring(str.Length - length);
}
else
{
str = str.PadLeft(length,
'0'
);
}
return
str;
}
/// <summary>
/// 解析段字符
/// </summary>
/// <param name="strNo"></param>
/// <returns></returns>
public
static
int
GetShortNum(
string
strNo)
{
int
num = 0;
for
(
int
i = 0; i < strNo.Length; i++)
{
num += BASE_CHAR.IndexOf(strNo[i]) * (
int
)Math.Pow(BASE_CHAR.Length, strNo.Length - i - 1);
}
return
num;
}
}
|
实例分析
原订单号组成规则 1010101001 {年}{月}{日}{一天流水号} 长度为10位 它可表示00-00-00 到 99-12-31 (100年 约36500天)的时间 和一天内9999个订单不重复
而使用36进制 {前3位表示日期}{后三位表示流水号} 长度6位 可表示46656天(约128年) 46656个流水号
欢迎转载,转载请注明:转载自[ http://www.cnblogs.com/zjfree/ ]
import java.util.*;
public class testid{
private String strYear=null;
private String strMonth=null;
private String strDate=null;
private String Syshour=null;
private String Sysmin=null;
private String Syssec=null;
private String SysDate=null;
private String localtime=null;
private String enter_id=null;
public void settime(){
Calendar ca = Calendar.getInstance();
strYear=String.valueOf(ca.get(Calendar.YEAR));
strMonth=String.valueOf(ca.get(Calendar.MONTH)+1);
strDate=String.valueOf(ca.get(Calendar.DATE));
if((ca.get(Calendar.MONTH)+1)<=9&&ca.get(Calendar.DATE)<=9)
{
SysDate=strYear+"0"+strMonth+"0"+strDate;
}else
if((ca.get(Calendar.MONTH)+1)>9&&ca.get(Calendar.DATE)>9)
{
SysDate=strYear+strMonth+strDate;
}else
if((ca.get(Calendar.MONTH)+1)<=9&&ca.get(Calendar.DATE)>9)
{
SysDate=strYear+"0"+strMonth+strDate;
}else
if((ca.get(Calendar.MONTH)+1)>9&&ca.get(Calendar.DATE)<=9)
{
SysDate=strYear+strMonth+"0"+strDate;
}
Syshour=String.valueOf(ca.get(Calendar.HOUR_OF_DAY));
Sysmin=String.valueOf(ca.get(Calendar.MINUTE));
Syssec=String.valueOf(ca.get(Calendar.SECOND));
if(ca.get(Calendar.HOUR_OF_DAY)<=9)
{
localtime=SysDate+"0"+ Syshour;
}
else
{
localtime=SysDate+Syshour;
}
if(ca.get(Calendar.MINUTE)<=9)
{
localtime=localtime+"0"+ Sysmin;
}
else
{
localtime+=Sysmin;
}
if(ca.get(Calendar.SECOND)<=9)
{
localtime=localtime+"0"+ Syssec;
}
else
{
localtime+=Syssec;
}
}
public void neikeid()
{
enter_id="nk"+localtime;
}
public void waikeid()
{
enter_id="wk"+localtime;
}
public void fuchankeid()
{
enter_id="fck"+localtime;
}
public void gukeid()
{
enter_id="gk"+localtime;
}
public void erkeid()
{
enter_id="ek"+localtime;
}
public void wuguankeid()
{
enter_id="wgk"+localtime;
}
public void print(String id)
{
System.out.println(id);
}
public static void main(String[] args)
{
testid ti=new testid();
ti.settime();
ti.wuguankeid();
ti.print(ti.enter_id);
}
}