SoFunction
Updated on 2025-03-06

Java generates an order number composed of time

Java generates an order number composed of time

There are many ways to implement order numbers, such as using UUID, Random or timestamp. The following explains how to implement them using SimpleDateFormat, and the length can be increased at will.

Reference code:

public static void main(String args[]) { 
	String strNumber = ("yyyyMMddHHmmssSSSSSSS");
    ("serial number:" + strNumber); 
} 
public static String customFormatDate(String dateFormat) {
    SimpleDateFormat simpleDateFormat;
    simpleDateFormat = new SimpleDateFormat(dateFormat);
    Date date = new Date();
    String str = (date);
    return str;
}

Output result:

Number: 202206211546350000989

Java randomly generates order numbers

 @ApiOperation("Random Order Number")
    private String generateOrderNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        return ((new Date()) + makeUUID(6)).toUpperCase();
    }
    @ApiOperation("Random String")
    private String makeUUID(int len) {
        return ().toString().replaceAll("-", "").substring(0, len);
    }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.