Here you can find the source of getDateSafe()
public static Date getDateSafe()
//package com.java2s; /*/*w w w. j a v a 2 s .c o m*/ * Copyright (C) 2008-2012, fluid Operations AG * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import java.util.Date; import java.util.concurrent.atomic.AtomicLong; public class Main { /** * The timestamp that was used for the last Context. We compare to this * value to have unique/synchronized values across different contexts */ private static AtomicLong lastTimestampUsed = new AtomicLong(0L); /** * Returns a unique Date considering the last stored timestamp. If the current * timestamp is equal to the last one, one millisecond is added. The last step is * repeated until a distinct timestamp is found. * * @return a unique Date * @return */ public static Date getDateSafe() { return new Date(getTimestampSafe()); } /** * Returns a unique timestamp considering the last stored timestamp. If the current * timestamp is equal to the last one, one millisecond is added. The last step is * repeated until a distinct timestamp is found. * * @return a unique timestamp */ public static long getTimestampSafe() { long timestamp = System.currentTimeMillis(); boolean repeat = true; while (repeat) { long lastUsed = lastTimestampUsed.get(); if (timestamp > lastUsed) { repeat = !lastTimestampUsed.compareAndSet(lastUsed, timestamp); } else { timestamp = lastUsed + 1; } } return timestamp; } }