Java Calendar Format format(Calendar calendar)

Here you can find the source of format(Calendar calendar)

Description

format

License

Apache License

Declaration

public static String format(Calendar calendar) 

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());
    }//from   w w w . j  av  a  2  s  .c o m

    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(Calendar cal, String pattern)
  2. format(Calendar calendar)
  3. format(Calendar calendar)
  4. format(Calendar calendar)
  5. format(Calendar calendar)
  6. format(Calendar calendar)
  7. format(Calendar calendar, String format)
  8. format(Calendar calendar, String format)
  9. format(Calendar calendar, String formatString)