Java Timestamp Format timestampFormat(final Date date)

Here you can find the source of timestampFormat(final Date date)

Description

timestamp Format

License

Apache License

Declaration

public static String timestampFormat(final Date date) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static final Map<String, ThreadLocal<SimpleDateFormat>> timestampFormatPool = new HashMap<String, ThreadLocal<SimpleDateFormat>>();
    private static final Object timestampFormatLock = new Object();
    private static String timestampPattern = "yyyy-MM-dd HH:mm:ss";

    public static String timestampFormat(final Date date) {
        if (date == null) {
            return "";
        }//from  w w w .  ja v a  2 s.  c  o m
        //      return timestampFormat.format(date);
        return getTimestampFormat().format(date);
    }

    public static String timestampFormat(final long datetime) {
        //      return timestampFormat.format(new Date(datetime));
        return getTimestampFormat().format(new Date(datetime));
    }

    private static SimpleDateFormat getTimestampFormat() {
        ThreadLocal<SimpleDateFormat> tl = timestampFormatPool.get(timestampPattern);
        if (null == tl) {
            synchronized (timestampFormatLock) {
                tl = timestampFormatPool.get(timestampPattern);
                if (null == tl) {
                    tl = new ThreadLocal<SimpleDateFormat>() {
                        @Override
                        protected synchronized SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(timestampPattern);
                        }
                    };
                    timestampFormatPool.put(timestampPattern, tl);
                }
            }
        }
        return tl.get();
    }
}

Related

  1. timestamp2String(long source, DateFormat format)
  2. Timestamp2UTC(Timestamp mytime)
  3. timestampConvertToString(int timeStamp, String format)
  4. timeStampForFileName(final String simpleDateFormat)
  5. timestampFormat(Date date)
  6. timeStampFormat(Timestamp t)
  7. timestampToFormattedDate(Long timestamp)
  8. timestampToString(final Date ts, final String format, final String tzId)
  9. timestampToString(Long timestamp, String formatStr)