privatejava
String fmtLong(Long val, web
intapp
size){ 函數
StringBuilder sb = ui
newthis
StringBuilder(google
""spa
); code
sb.append(val); htm
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);
}
}