Java Date Now getCurrentTime(String pattern)

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

Description

get Current Time

License

Apache License

Declaration

public static String getCurrentTime(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 Object lock = new Object();
    private static Map<String, ThreadLocal<SimpleDateFormat>> localThreadMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();
    public final static String PATTERN_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";

    public static String getCurrentTime() {
        return getCurrentTime(PATTERN_YYYYMMDDHHMM);
    }// w w  w  .  ja v a  2 s .c o m

    public static String getCurrentTime(String pattern) {
        SimpleDateFormat format = getDateFormat(pattern);
        return format.format(new Date());
    }

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

Related

  1. getCurrentTime(String format)
  2. getCurrentTime(String format)
  3. getCurrentTime(String formatter)
  4. getCurrentTime(String pattern)
  5. getCurrentTime(String pattern)
  6. getCurrentTime(String pattern)
  7. getCurrentTime(String timeFormat)
  8. getCurrentTime2MysqlDateTime()
  9. getCurrentTimeAndDate()