package com.srbenoit.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * A thread-safe singleton class to perform date to string conversion. This is needed as the <code>
 * SimpleDateFormat</code> class is not thread safe.
 */
public final class SharedDateFormat {

    /** formatter for dates */
    private final static SimpleDateFormat DATEFORMAT;

    static {
        DATEFORMAT = new SimpleDateFormat("MM/dd/yy HH:mm:ss.SSS", Locale.getDefault());
    }

    /**
     * Private constructor to enforce singleton model.
     */
    private SharedDateFormat() {

        // No action
    }

    /**
     * Generates the string representation of a date value.
     *
     * @param   date  the date
     * @return  the String representation
     */
    public static String format(final Date date) {

        synchronized (DATEFORMAT) {
            return DATEFORMAT.format(date);
        }
    }

    /**
     * Generates the string representation of a timestamp value.
     *
     * @param   timestamp  the timestamp
     * @return  the String representation
     */
    public static String format(final long timestamp) {

        return format(new Date(timestamp));
    }
}
