Java yyyy formatYMD(Date d)

Here you can find the source of formatYMD(Date d)

Description

format YMD

License

Open Source License

Declaration

public static String formatYMD(Date d) 

Method Source Code


//package com.java2s;
import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    public static final String YMD = "yyyy-MM-dd";
    private static final Map<String, ThreadLocal<DateFormat>> tlDateFormatMap = new ConcurrentHashMap<String, ThreadLocal<DateFormat>>();

    public static String formatYMD(Date d) {
        return getDateFormat(YMD).format(d);
    }//from ww w.j  a va2  s  .c  om

    public static String format(Date date, String fmt) {
        return getDateFormat(fmt).format(date);
    }

    public static DateFormat getDateFormat(String fmt) {
        ThreadLocal<DateFormat> tl = tlDateFormatMap.get(fmt);
        if (tl == null) {
            tl = threadLocalDateFormat(fmt);
            tlDateFormatMap.put(fmt, tl);
        }
        return tl.get();
    }

    private static ThreadLocal<DateFormat> threadLocalDateFormat(final String pattern) {
        ThreadLocal<DateFormat> tl = new ThreadLocal<DateFormat>() {
            protected DateFormat initialValue() {
                return new SimpleDateFormat(pattern);
            }
        };
        return tl;
    }
}

Related

  1. formatter()
  2. formatTimeSpan(long time)
  3. formatToEapoDate(Date date)
  4. formatToSqlDate(final Date date)
  5. formatToString(String format, long time)
  6. formatYmd(Date d)