package com.train.first;import java.text.SimpleDateFormat; import java.util.Date;public class Test { private static int i = 0;
public static void main(String[] args) throws Exception { for (int i = 0; i < 1001; i++) { System.out.println(getNext()); } }
public static String getNext() throws Exception { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); String today = formatter.format(new Date());
String next = String.valueOf(i++);
switch (next.length()) { case 1: next = "00" + next; break;
case 2: next = "0" + next; break;
case 3: break; default: throw new Exception("超过最大值"); }
return today + next; } }
public class TestSequence { /** * @param args */ private static int i = 0; public static void main(String[] args) { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); String today = formatter.format(new Date()); Scanner s = new Scanner(System.in); int next = s.nextInt(); for(int x = 1;x<=next;x++) { System.out.println(today+getSequence(x)); }
public static void main(String[] args) throws InterruptedException { SerialNumber serial = new FileEveryDaySerialNumber(5, "EveryDaySerialNumber.dat"); while(true) { System.out.println(serial.getSerialNumber()); TimeUnit.SECONDS.sleep(2); } } } abstract class SerialNumber { public synchronized String getSerialNumber() { return process(); } protected abstract String process(); } abstract class EveryDaySerialNumber extends SerialNumber {
protected final static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); protected DecimalFormat df = null;
public EveryDaySerialNumber(int width) { if(width < 1) { throw new IllegalArgumentException("Parameter length must be great than 1!"); } char[] chs = new char[width]; for(int i = 0; i < width; i++) { chs[i] = '0'; } df = new DecimalFormat(new String(chs)); }
protected String process() { Date date = new Date(); int n = getOrUpdateNumber(date, 1); return format(date) + format(n); }
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.format(new Date());
返回2008-11-06在加上你的什么流水
import java.text.*; public class TestDate {
public static void main (String args[]){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
System.out.print(dateFormat.format(date));
}
}
3位流水号什么意思 我不知道 楼主说清楚点 是不是什么根据时间生成的随机数啊?
package com.train.first;import java.text.SimpleDateFormat;
import java.util.Date;public class Test
{
private static int i = 0;
public static void main(String[] args) throws Exception
{
for (int i = 0; i < 1001; i++)
{
System.out.println(getNext());
}
}
public static String getNext() throws Exception
{
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String today = formatter.format(new Date());
String next = String.valueOf(i++);
switch (next.length())
{
case 1:
next = "00" + next;
break;
case 2:
next = "0" + next;
break;
case 3:
break; default:
throw new Exception("超过最大值");
}
return today + next;
}
}
* @param args
*/
private static int i = 0;
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String today = formatter.format(new Date());
Scanner s = new Scanner(System.in);
int next = s.nextInt();
for(int x = 1;x<=next;x++)
{
System.out.println(today+getSequence(x));
}
}
private static String getSequence(int nextInt) {
String str = "";
if(0<nextInt&&nextInt<10)
{
str = "00"+nextInt;
}
else if(10<=nextInt&&nextInt<100)
{
str = "0"+nextInt;
}
else
{
str = String.valueOf(nextInt);
}
return str;
}
}
按照你的要求,会根据你所输入的一个数字,产生从0开始到该数字结尾的当前日期+3位流水编码.
下次运行哪怕是重启后,得到的序列得是 20081107002。我在前几个月 CSDN 上回复过一个一模一样的问题,把代码复制过来了,可以参考一下。import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;public class Test01 {
public static void main(String[] args) throws InterruptedException {
SerialNumber serial = new FileEveryDaySerialNumber(5, "EveryDaySerialNumber.dat");
while(true) {
System.out.println(serial.getSerialNumber());
TimeUnit.SECONDS.sleep(2);
}
}
}
abstract class SerialNumber { public synchronized String getSerialNumber() {
return process();
}
protected abstract String process();
}
abstract class EveryDaySerialNumber extends SerialNumber {
protected final static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
protected DecimalFormat df = null;
public EveryDaySerialNumber(int width) {
if(width < 1) {
throw new IllegalArgumentException("Parameter length must be great than 1!");
}
char[] chs = new char[width];
for(int i = 0; i < width; i++) {
chs[i] = '0';
}
df = new DecimalFormat(new String(chs));
}
protected String process() {
Date date = new Date();
int n = getOrUpdateNumber(date, 1);
return format(date) + format(n);
}
protected String format(Date date) {
return sdf.format(date);
}
protected String format(int num) {
return df.format(num);
}
/**
* 获得序列号,同时更新持久化存储中的序列
* @param current 当前的日期
* @param start 初始化的序号
* @return 所获得新的序列号
*/
protected abstract int getOrUpdateNumber(Date current, int start);
}
class FileEveryDaySerialNumber extends EveryDaySerialNumber { /**
* 持久化存储的文件
*/
private File file = null;
/**
* 存储的分隔符
*/
private final static String FIELD_SEPARATOR = ","; public FileEveryDaySerialNumber(int width, String filename) {
super(width);
file = new File(filename);
} @Override
protected int getOrUpdateNumber(Date current, int start) {
String date = format(current);
int num = start;
if(file.exists()) {
List<String> list = FileUtil.readList(file);
String[] data = list.get(0).split(FIELD_SEPARATOR);
if(date.equals(data[0])) {
num = Integer.parseInt(data[1]);
}
}
FileUtil.rewrite(file, date + FIELD_SEPARATOR + (num + 1));
return num;
}
}
class FileUtil { public static void rewrite(File file, String data) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
bw.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bw != null) {
try {
bw.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
public static List<String> readList(File file) {
BufferedReader br = null;
List<String> data = new ArrayList<String>();
try {
br = new BufferedReader(new FileReader(file));
for(String str = null; (str = br.readLine()) != null; ) {
data.add(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(br != null) {
try {
br.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
return data;
}
}
// TODO Auto-generated method stub
DecimalFormat df = new DecimalFormat("000");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
for(int i=0;i<1000;i++){
StringBuilder packIDBuilder = new StringBuilder();
packIDBuilder.append(sdf.format(new Date()));
packIDBuilder.append(df.format(i));
System.out.println(packIDBuilder.toString());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DecimalFormat df = new DecimalFormat("000");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
for(int i=0;i<1000;i++){
StringBuilder packIDBuilder = new StringBuilder();
packIDBuilder.append(sdf.format(new Date()));
Random rd = new Random();
packIDBuilder.append(rd.nextInt(1000));
System.out.println(packIDBuilder.toString());
}
}