Java Hour Format format(Date date)

Here you can find the source of format(Date date)

Description

format

License

Apache License

Declaration

public static String format(Date date) 

Method Source Code

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

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final String a = "yyyy-MM-dd HH:mm:ss:SSS";
    private static final String e = "yyyy-MM-dd HH:mm:ss";
    private static final ThreadLocal<Map<String, SimpleDateFormat>> threadLocal = new ThreadLocal<Map<String, SimpleDateFormat>>();

    public static String format() {
        return getDateFormat(a).format(new Date());
    }//w ww. ja  v a2  s.c om

    public static String format(String s) {
        return getDateFormat(s).format(new Date());
    }

    public static String format(Calendar calendar) {
        return getDateFormat(a).format(calendar.getTime());
    }

    public static String format(Calendar calendar, String s) {
        return getDateFormat(s).format(calendar.getTime());
    }

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

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

    private static SimpleDateFormat getDateFormat(String format) {
        Map<String, SimpleDateFormat> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, SimpleDateFormat>();
            threadLocal.set(map);
        }
        SimpleDateFormat f = map.get(format);
        if (f == null) {
            f = new SimpleDateFormat(format);
            map.put(format, f);
        }
        return f;
    }
}

Related

  1. format(Date date)
  2. format(Date date)
  3. format(Date date)
  4. format(Date date)
  5. format(Date date)
  6. format(Date date)
  7. format(Date date)
  8. format(Date date)
  9. format(Date date)