Java trong vòng lặp là nhanh hơn một giây nó sẽ ở lại cùng để đảm bảo luôn luôn độc đáo đặc biệt đa luồng chức năng của nó.
Sử dụng somthing như thế này
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss:SSSSSSS");
Một số gợi ý để bạn wont đi vào nhiều rắc rối debug
printDate("dd.MM.yyyy HH:mm:ss.SSS");//02.05.2010 21:45:58.073
printDate("dd.MM.yyyy HH:mm:ss.SSSSSS");//02.05.2010 21:45:58.000073
printDate("dd.MM.yyyy HH:mm:ss.SSS'000'");//02.05.2010 21:45:58.073000
printDate("dd.MM.yyyy HH:mm:ss.'000000'");//02.05.2010 21:45:58.000000
tryToParseDate("dd.MM.yyyy HH:mm:ss.SSS");//good
tryToParseDate("dd.MM.yyyy HH:mm:ss.SSSSSS");//good
tryToParseDate("dd.MM.yyyy HH:mm:ss.SSS'000'");//bad
tryToParseDate("dd.MM.yyyy HH:mm:ss.'000000'");//good
tham khảo:
@slartidan trả lời
String-Date conversion with nanoseconds
Như Khuyến nghị khi tôi phải đối mặt với tình trạng này:
1)Nếu đang gọi từ S3 AWS
file nó nên được đặt tên như là duy nhất lúc bắt đầu của tập tin tên nó sẽ làm băm và tìm kiếm khá nhanh. như từ các thực hành tốt nhất của AWS S3 để tối ưu hóa.
public static String genarateFileName(String name) {
StringBuilder sb = new StringBuilder(name);
sb.insert(0, IdUtil.getUniqueUuid());in short to increase performance of S3 put and get etc..)
if (sb.lastIndexOf(".") != -1) {
sb.insert(sb.lastIndexOf("."), "_" + System.nanoTime());
} else {
sb.append("_").append(System.nanoTime());
}
return sb.toString();
}
2)Để tạo nano ngẫu nhiên
public static String getUniqueUuid() {
int rand = (int) (Math.random() * 100);
return Integer.toHexString(rand) + Long.toHexString(java.lang.System.nanoTime());
}
3) tôi sử dụng cả hai nano ngẫu nhiên với kiểm tra chuỗi ngẫu nhiên dưới đây tạo ra chuỗi ngẫu nhiên của một số chiều dài
/**
* Generate a random uuid of the specified length. Example: uuid(15) returns
* "VcydxgltxrVZSTV"
*
* @param len the desired number of characters
* @return
*/
public static String uuid(int len) {
return uuid(len, CHARS.length);
}
/**
* Generate a random uuid of the specified length, and radix. Examples: <ul>
* <li>uuid(8, 2) returns "01001010" (8 character ID, base=2) <li>uuid(8,
* 10) returns "47473046" (8 character ID, base=10) <li>uuid(8, 16) returns
* "098F4D35" (8 character ID, base=16) </ul>
*
* @param len the desired number of characters
* @param radix the number of allowable values for each character (must be
* <= 62)
* @return
*/
public static String uuid(int len, int radix) {
if (radix > CHARS.length) {
throw new IllegalArgumentException();
}
char[] uuid = new char[len];
// Compact form
for (int i = 0; i < len; i++) {
uuid[i] = CHARS[(int) (Math.random() * radix)];
}
return new String(uuid);
}
Java 8 giới thiệu các API mới cho Date and Time. Bạn có thể xem nhanh tại đây: http://www.baeldung.com/java-8-date-time-intro – Touniouk
bạn phải sử dụng milli giây và nếu bạn đang sử dụng các chủ đề sử dụng nano giây – shareef
Bạn có thời gian chờ là vài giây. Bạn có chắc chắn mã không được thực thi trong cùng một giây với mili giây chênh lệch không ?. Tôi sẽ khuyên bạn nên sử dụng System.currentTimeMillis() cho một phần duy nhất của tên tệp. –