1. java自動生成編號
private static int m=1;//全局靜態變數,(必須static修飾)每調用一次方法自增1
public String add(){
addin(m);
return "addTiao";
}
public void addin(int a){
String str1="A";
Date date=new Date();
String str2=new SimpleDateFormat("yyyyMMdd"). format(date);//自動生成日期
int datee=Integer.parseInt(str2);
inlist=instockService.getInListDown();//按降冪排序
instockEntity=(InstockEntity) inlist.get(0);
String ss=instockEntity.getInstock_number();//得到最大日期
if(ss!=null){
String riqi=ss.substring(1, 9);
int riqi1=Integer.parseInt(riqi);
if(datee>riqi1){
a=1;//如果日期變大則初始化訂單號
}
}
String str3=null;
if(a>=0&&a<=9){
str3="00"+String.valueOf(a);
}else if(a>=10&&a<=99){
str3="0"+String.valueOf(a);
}else if(a>=100&&a<=999){
str3=String.valueOf(a);
}
String instock_number=str1+str2+str3;
instockEntity.setInstock_number(instock_number);
instockService.add(instockEntity);
m=a+1;
}
2. 編號以0000-9999這種方式自動生成,java類應該怎麼寫
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getNumber(9999));
}
public static String getNumber(int num)
{
num++;
String result = "";
switch ((num+"").length()) {
case 1:
result = "000" + num;
break;
case 2:
result = "0" + num;
break;
case 3:
result = "0" + num;
break;
case 4:
result = "" + num;
break;
// 此處代表編號已經超過了9999,從0重新開始
default:
result = "0000";
break;
}
return result;
}
3. java濡備綍鏍規嵁鏃ユ湡鑷鍔ㄧ紪鍙
瑕佹牴鎹鏃墮棿鐢熸垚緙栧彿錛岄栧厛瑕佺煡閬撶紪鍙風殑鏂瑰紡錛屾瘮濡傚彧鏄鎶婃椂闂翠腑鐨勭﹀彿鍘繪帀鍙浠ヤ嬌鐢⊿impleDateFromat榪欎釜綾
//鍏跺疄鏈夊緢澶氭儏鍐甸兘鍙浠ョ敤瀹冭В鍐籌紝鍙闇瑕佹敼鍏朵腑鐨勬牸寮
//澶ф傛槸榪欐牱錛屾墜鍐欑殑浠g爜
Stringbh=newSimpleDateFormate("yyyyMMddHHmmss").format(newDate());
//寰楀埌201806061331
濡傛灉闇瑕佸嶆潅鐐圭殑錛屽彲浠ヤ嬌鐢–alendar榪欎釜綾伙紝鑾峰彇闇瑕佺紪鍙風殑鏁版嵁榪涜岀紪鍙
Calendarc=Calendar.getInstance();
c.setTime(newDate());
//閫氳繃get鏂規硶鑾峰彇鍏蜂綋鐨勫礆紝騫淬佹湀銆佹棩銆佹椂銆佸垎銆佺掋佹槦鏈絳
c.get(Calendar.HOUR_OF_DAY);//鑾峰緱灝忔椂錛屽叾浠栫殑甯擱噺鍙浠ュ湪瀹樻柟API鏌ユ壘錛屾垨鑰呯櫨搴︺怌alendar.java甯擱噺銆