Java Locale Format format(final Date date, final String format)

Here you can find the source of format(final Date date, final String format)

Description

Formats a Date according to the first format in the array.

License

LGPL

Parameter

Parameter Description
date The date to format.
format The date format to use.

Return

The formatted date.

Declaration

public static String format(final Date date, final String format) 

Method Source Code

//package com.java2s;
/**//  www .  j  av a 2  s  .  co  m
 * Copyright 2005-2008 Noelios Technologies.
 * 
 * The contents of this file are subject to the terms of the following open
 * source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 (the "Licenses"). You can
 * select the license that you prefer but you may not use this file except in
 * compliance with one of these Licenses.
 * 
 * You can obtain a copy of the LGPL 3.0 license at
 * http://www.gnu.org/licenses/lgpl-3.0.html
 * 
 * You can obtain a copy of the LGPL 2.1 license at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 * 
 * You can obtain a copy of the CDDL 1.0 license at
 * http://www.sun.com/cddl/cddl.html
 * 
 * See the Licenses for the specific language governing permissions and
 * limitations under the Licenses.
 * 
 * Alternatively, you can obtain a royaltee free commercial license with less
 * limitations, transferable or non-transferable, directly at
 * http://www.noelios.com/products/restlet-engine
 * 
 * Restlet is a registered trademark of Noelios Technologies.
 */

import java.text.SimpleDateFormat;

import java.util.Date;

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

public class Main {
    /** Remember the often used GMT time zone. */
    private static final TimeZone TIMEZONE_GMT = TimeZone.getTimeZone("GMT");

    /**
     * Formats a Date according to the first format in the array.
     * 
     * @param date
     *            The date to format.
     * @param format
     *            The date format to use.
     * @return The formatted date.
     */
    public static String format(final Date date, final String format) {
        if (date == null) {
            throw new IllegalArgumentException("Date is null");
        }

        final SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.US);
        formatter.setTimeZone(TIMEZONE_GMT);
        return formatter.format(date);
    }
}

Related

  1. format(double number)
  2. format(double number, int fractionDigits)
  3. format(double value)
  4. format(double values, boolean doNotFormat)
  5. format(final Date date, final String format)
  6. format(final double num, final int prec)
  7. format(final String bundleKey, final String messageKey, final Locale locale, final Object... arguments)
  8. format(int[] a)
  9. format(Locale locale, ResourceBundle bundle, String key, Object... args)