Java in a loop is faster than one second, it will remain the same to make sure that it is always unique especially in a multi-line function .
Use something like this
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss:SSSSSSS");
Some tips so you don't run into a big debugging problem
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
Reference:
@slartidan Answer
Nanosecond String-Date Conversion
As a recommendation, when I came across this situation:
1) If you call from S3 AWS
it should be called unique when starting the file name, it will do hashing and searching pretty quickly. as one of AWS S3 best practices for optimization.
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) To create random nano
public static String getUniqueUuid() { int rand = (int) (Math.random() * 100); return Integer.toHexString(rand) + Long.toHexString(java.lang.System.nanoTime()); }
3) I use as random nano with random string check below, generate random string of some length
public static String uuid(int len) { return uuid(len, CHARS.length); } public static String uuid(int len, int radix) { if (radix > CHARS.length) { throw new IllegalArgumentException(); } char[] uuid = new char[len];
source share