Java Locale Format formatDateTime(java.util.Date date, String format, String locale, String timeZone)

Here you can find the source of formatDateTime(java.util.Date date, String format, String locale, String timeZone)

Description

Formats a date using a format string.

License

Mozilla Public License

Parameter

Parameter Description
date the date to format
format the format string
locale the locale
timeZone the timezone

Return

the formatted date

Declaration

public static String formatDateTime(java.util.Date date, String format, String locale, String timeZone) 

Method Source Code

//package com.java2s;
/*//from   w  ww . j a v  a 2 s  .c om
 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
 * and the EPL 1.0 (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 * Iso8601:
 * Initial Developer: Robert Rathsack (firstName dot lastName at gmx dot de)
 */

import java.text.SimpleDateFormat;

import java.util.Locale;
import java.util.TimeZone;

public class Main {
    /**
     * Formats a date using a format string.
     *
     * @param date the date to format
     * @param format the format string
     * @param locale the locale
     * @param timeZone the timezone
     * @return the formatted date
     */
    public static String formatDateTime(java.util.Date date, String format, String locale, String timeZone) {
        SimpleDateFormat dateFormat = getDateFormat(format, locale, timeZone);
        synchronized (dateFormat) {
            return dateFormat.format(date);
        }
    }

    private static SimpleDateFormat getDateFormat(String format, String locale, String timeZone) {
        try {
            // currently, a new instance is create for each call
            // however, could cache the last few instances
            SimpleDateFormat df;
            if (locale == null) {
                df = new SimpleDateFormat(format);
            } else {
                Locale l = new Locale(locale);
                df = new SimpleDateFormat(format, l);
            }
            if (timeZone != null) {
                df.setTimeZone(TimeZone.getTimeZone(timeZone));
            }
            return df;
        } catch (Exception e) {
            throw new RuntimeException("PARSE_ERROR:" + (format + "/" + locale + "/" + timeZone), e);
        }
    }
}

Related

  1. formatDate(String value, Locale locale)
  2. formatDate2Date(Date date, String format)
  3. formatDate2String(Date date, String format)
  4. formatDateByFormat(Date date, String format)
  5. formatDateTime(final Date date)
  6. formatDateToSQLString(Date srcDate)
  7. formatDecimal(BigInteger b)
  8. formatDecimalDisplay(final double _numberToFormat, final String _formatToApply)
  9. formatDouble(double in)