Java Locale Format formatDate(Calendar cal, String format)

Here you can find the source of formatDate(Calendar cal, String format)

Description

Formats the date held in a Calendar using the current locale's timezone (not the calendar's own timezone).

License

Apache License

Declaration

public static String formatDate(Calendar cal, 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 w w  .  j a  v a  2  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. formataValor(Double valor)
  2. formatBytes(final long bytes)
  3. formatBytes(long byteCount, Locale locale)
  4. formatBytesForDisplay(long amount)
  5. formatComma(String number)
  6. formatDate(Date date, String format)
  7. formatDate(Date date, String format)
  8. formatDate(Date date, String format, String localeText)
  9. formatDate(Date date, String pattern)