Java Now nowString(String pattern)

Here you can find the source of nowString(String pattern)

Description

now String

License

Apache License

Declaration

public static String nowString(String pattern) 

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>> pool = new HashMap<String, ThreadLocal<SimpleDateFormat>>();
    private static final Object lock = new Object();

    public static String nowString(String pattern) {
        return getDateFormat(pattern).format(new Date());
    }/*from ww w  . j a v  a2  s. c  o  m*/

    public static SimpleDateFormat getDateFormat(String pattern) {
        ThreadLocal<SimpleDateFormat> tl = pool.get(pattern);
        if (tl == null) {
            synchronized (lock) {
                tl = pool.get(pattern);
                if (tl == null) {
                    final String p = pattern;
                    tl = new ThreadLocal<SimpleDateFormat>() {
                        protected synchronized SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(p);
                        }
                    };
                    pool.put(p, tl);
                }
            }
        }
        return tl.get();
    }
}

Related

  1. nowFormat(String format)
  2. nowIdentity()
  3. nowInBasicFormat()
  4. nowStr()
  5. nowString(String pattern)
  6. nowTime(String formartStr)
  7. nowTime(String format)
  8. printNow()
  9. stringDate(Date now)