時間戳id生成器

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;

public class IdGenerator {
    static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

    public static String timestamp() {
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(formatter);
        return format + (ThreadLocalRandom.current().nextInt(9999) + 1);
    }

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(formatter);
        for (int i = 0; i < 10; i++) {
            System.out.println(format + (ThreadLocalRandom.current().nextInt(9999) + 1));
        }
    }
   
}
相關文章
相關標籤/搜索