Java Long Number to Date formatDate(long time, String format)

Here you can find the source of formatDate(long time, String format)

Description

Formats a Java timestamp (millis since epoch) using the current locale's timezone.

License

Apache License

Declaration

public static String formatDate(long time, String format) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    private static String localZone = TimeZone.getDefault().getID();
    private static Map<String, Map<String, ThreadLocal<SimpleDateFormat>>> tzDateFormatters = new ConcurrentHashMap<String, Map<String, ThreadLocal<SimpleDateFormat>>>();

    /**/*from w ww  .  ja va2 s.c o m*/
     *  Formats a date using a <code>SimpleDateFormat</code> instance with the
     *  current locale's timezone.
     */
    public static String formatDate(Date date, String format) {
        return formatDate(date, format, localZone);
    }

    /**
     *  Formats a date using a <code>SimpleDateFormat</code> instance with the
     *  specified timezone.
     */
    public static String formatDate(Date date, String format, String zoneId) {
        return getDateFormatter(format, zoneId).format(date);
    }

    /**
     *  Formats the date held in a <code>Calendar</code> using the current
     *  locale's timezone (<em>not</em> the calendar's own timezone).
     */
    public static String formatDate(Calendar cal, String format) {
        return formatDate(cal.getTime(), format, localZone);
    }

    /**
     *  Formats the date held in a <code>Calendar</code> using the specified
     *  timezone (<em>not</em> the calendar's own timezone).
     */
    public static String formatDate(Calendar cal, String format, String zoneId) {
        return getDateFormatter(format, zoneId).format(cal.getTime());
    }

    /**
     *  Formats a Java timestamp (millis since epoch) using the current
     *  locale's timezone.
     */
    public static String formatDate(long time, String format) {
        return formatDate(new Date(time), format, localZone);
    }

    /**
     *  Formats a Java timestamp (millis since epoch) using the specified
     *  timezone (<em>not</em> the calendar's own timezone).
     */
    public static String formatDate(long time, String format, String zoneId) {
        return getDateFormatter(format, zoneId).format(new Date(time));
    }

    private static SimpleDateFormat getDateFormatter(final String format, final String zoneId) {
        Map<String, ThreadLocal<SimpleDateFormat>> zoneFormatters = tzDateFormatters.get(zoneId);
        if (zoneFormatters == null) {
            zoneFormatters = new ConcurrentHashMap<String, ThreadLocal<SimpleDateFormat>>();
            tzDateFormatters.put(zoneId, zoneFormatters);
        }

        ThreadLocal<SimpleDateFormat> threadLocal = zoneFormatters.get(format);
        if (threadLocal == null) {
            threadLocal = new ThreadLocal<SimpleDateFormat>() {
                @Override
                protected SimpleDateFormat initialValue() {
                    SimpleDateFormat formatter = new SimpleDateFormat(format);
                    formatter.setTimeZone(TimeZone.getTimeZone(zoneId));
                    return formatter;
                }
            };
            zoneFormatters.put(format, threadLocal);
        }
        return threadLocal.get();
    }
}

Related

  1. formatDate(long t)
  2. formatDate(long time)
  3. formatDate(long time)
  4. formatDate(long time)
  5. formatDate(long time)
  6. formatDate(long time_millis)
  7. formatDate(long timeStamp)
  8. formatDate(long timestamp, String format)
  9. formatDate(String format, long timestamp)