Skip to main content

Posts

Showing posts from November, 2011

Creating Unique Values using Date

Many a times we are in need of a unique value. The best way to generate a unique value is to use the current date and time combination. We can add both the current date and time to generate a unique value but there are times when we need more precise data to get uniqueness. For instance, you have two jobs running in the same minute then your unique value fails, i.e. when one job is ran at 9.32 AM after 10 milli secs and another is ran at 9.32 AM after 20 milli secs. At this time our unique generator creates the values as: 20111102932 (2011 - 11 - 02 9:32) for both the orders. This is when we will need to include millisecs too. To get the milli secs you can use the following commands in Java:


long ldtInMillis = new Date().getTime();System.out.println("Date - "+ ldtInMillis);Calendar lCalDtMillis = Calendar.getInstance();System.out.println("Calender - " + lCalDtMillis.getTimeInMillis());