哪位高手帮忙做个Java题,要完整代码。生成18位数格式为:用户id(6为id位数不够前面补0)+时间(年月日时

发布网友 发布时间:2022-04-24 11:33

我来回答

3个回答

热心网友 时间:2023-10-11 08:02

等下帮你写

public static void main(String[] args) {
long id=1001;
StringBuffer sb = new StringBuffer();
sb.append(LongToFixLengthString(id));
sb.append(" ");
SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmm");
sb.append(df.format(new Date()));
sb.append(" ");
Random random = new Random();
sb.append(random.nextInt(100)+10);
System.out.println(sb.toString());
}
public static String LongToFixLengthString(long id) {
NumberFormat formatter = new DecimalFormat("000000");
return formatter.format(id);
}追问急用啊 速度 谢了

追答改成 sb.append(random.nextInt(90)+10);

热心网友 时间:2023-10-11 08:02

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Test {

public static void main(String[] args) {
int id = 123;

System.out.println(getStr(id));

}

public static String getStr(int id){
StringBuilder sb = new StringBuilder();

sb.append(id);

while(sb.length() < 6){
sb.insert(0, "0");
}

sb.append(new SimpleDateFormat("yyMMddHHmm").format(new Date()));
int number = new Random().nextInt(100);

if(number < 10){
sb.append(0);
}
sb.append(number);

return sb.toString();
}

}
--------testing
000123120223151746

热心网友 时间:2023-10-11 08:03

String id="113";

SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");

String time = sdf.format(new Date());

String rand = (int)(Math.random()*100)+"";

String res = id+" "+time+" "+rand;

for(int i =0;i<=21-res.length();i++)
{
res = "0"+res;
}

System.out.println(res);

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com