Java Millisecond Format format(long millis)

Here you can find the source of format(long millis)

Description

format

License

Open Source License

Declaration

public static String format(long millis) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

public class Main {
    public static final String ISO_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss", ISO_DATE_HOUR_FORMAT = "yyyy-MM-dd HH",
            ISO_DATE_FORMAT = "yyyy-MM-dd", ISO_HOUR_FORMAT = "HH";
    private static ThreadLocal<Map<String, DateFormat>> dateFormats = new ThreadLocal<Map<String, DateFormat>>() {
        public Map<String, DateFormat> get() {
            return super.get();
        }/*from w ww.ja  va 2s .c o  m*/

        protected Map<String, DateFormat> initialValue() {
            return new HashMap<String, DateFormat>();
        }

        public void remove() {
            super.remove();
        }

        public void set(Map<String, DateFormat> value) {
            super.set(value);
        }
    };

    public static String format(long millis, String format) {
        return getDateFormat(format).format(millis);
    }

    public static String format(long millis) {
        return format(millis, ISO_DATETIME_FORMAT);
    }

    private static DateFormat getDateFormat(String format) {
        Map<String, DateFormat> threadDateFormats = dateFormats.get();
        if (!threadDateFormats.containsKey(format)) {
            DateFormat dateFormat = new SimpleDateFormat(format);
            dateFormat.setLenient(false);
            dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            threadDateFormats.put(format, dateFormat);
        }
        return threadDateFormats.get(format);
    }
}

Related

  1. createDateTimeString(final long ms, final DateFormat timeFormat, final String milliSep)
  2. format(final long timeInMillis)
  3. format(long millis)
  4. format(long millis)
  5. format(long millis)
  6. format(long millis, String pattern)
  7. format(long milliseconds, String format)
  8. format(long timeMillis)