java中自动生成有规律的编号
答案:2 悬赏:0
解决时间 2021-12-12 16:32
- 提问者网友:唤魂
- 2021-12-11 16:51
java中自动生成有规律的编号,例如例如2015080001,2015080002,而且每个月的第一个编号必须是xxxxxx0001,前面六位是当前年月
最佳答案
- 二级知识专家网友:承载所有颓废
- 2021-12-11 17:16
实现思路:在本地存储一个文本文件用于记录上一次的编号序数,生成编号时从文件里读出编号序数后加一,并把结果保存回文件中以备下次生成使用。如此处理即可达成需求,具体代码如下(运行环境:jdk8或以上):
package com.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDate;
public class NoCreater {
public static void main(String[] args) {
System.out.println(next());
}
public static String next() {
File file = getCountFile();
try (BufferedReader fr = new BufferedReader(new FileReader(file))) {
int nextNum = Integer.valueOf(fr.readLine()) + 1;
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(String.valueOf(nextNum).getBytes());
}
String next = "";
if (nextNum / 10 < 1) {
next += "000" + nextNum;
} else if (nextNum / 100 < 1) {
next += "00" + nextNum;
} else if (nextNum / 1000 < 1) {
next += "0" + nextNum;
} else {
next += nextNum;
}
int year = LocalDate.now().getYear();
int month = LocalDate.now().getMonthValue();
return year + "" + month + next;
} catch (IOException e) {
e.printStackTrace();
return "生成编号出错";
}
}
private static File getCountFile() {
File file = new File("count.txt");
if (!file.exists()) {
try {
file.createNewFile();
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write("0".getBytes());
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return file;
}
}
package com.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDate;
public class NoCreater {
public static void main(String[] args) {
System.out.println(next());
}
public static String next() {
File file = getCountFile();
try (BufferedReader fr = new BufferedReader(new FileReader(file))) {
int nextNum = Integer.valueOf(fr.readLine()) + 1;
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(String.valueOf(nextNum).getBytes());
}
String next = "";
if (nextNum / 10 < 1) {
next += "000" + nextNum;
} else if (nextNum / 100 < 1) {
next += "00" + nextNum;
} else if (nextNum / 1000 < 1) {
next += "0" + nextNum;
} else {
next += nextNum;
}
int year = LocalDate.now().getYear();
int month = LocalDate.now().getMonthValue();
return year + "" + month + next;
} catch (IOException e) {
e.printStackTrace();
return "生成编号出错";
}
}
private static File getCountFile() {
File file = new File("count.txt");
if (!file.exists()) {
try {
file.createNewFile();
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write("0".getBytes());
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return file;
}
}
全部回答
- 1楼网友:花一样艳美的陌生人
- 2021-12-11 18:54
public class main {
private static long n = 1;
public static void main(string[] args) {
system.out.println(test(n));
system.out.println(test(n));
}
public static long test(long l) {
string str = new simpledateformat("yyyymm")
.format(new java.util.date());
long m = long.parselong((str)) * 10000;
long ret = m + l;
n = l + 1;
return ret;
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯