Java Locale Format format(Date inputDate, String format, Locale locale)

Here you can find the source of format(Date inputDate, String format, Locale locale)

Description

Formats the date input to String using the format given.

License

Educational Community License

Parameter

Parameter Description
inputDate The date that needs to be formatted.
format The given date-time format.
locale The given locale.

Exception

Parameter Description
ParseException If not a valid date compared to ISO_ZONED_DATE_TIME format

Declaration

public static String format(Date inputDate, String format, Locale locale) 

Method Source Code

//package com.java2s;
/**********************************************************************************
 * $URL$/*from  ww w .j a v a  2 s .c  om*/
 * $Id$
 **********************************************************************************
 *
 * Copyright (c) 2008 The Sakai Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.opensource.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **********************************************************************************/

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Locale;

public class Main {
    /**
     * Formats the date input to String using the format given.
     * 
     * @param inputDate
     *            The date that needs to be formatted.
     * @param format
     *            The given date-time format.
     * @param locale
     *            The given locale.
     * @throws ParseException
     *          If not a valid date compared to ISO_ZONED_DATE_TIME format
     */
    public static String format(Date inputDate, String format, Locale locale) {
        SimpleDateFormat formatter = null;
        try {
            formatter = new SimpleDateFormat(format, locale);
            return formatter.format(inputDate);
        } catch (Exception ex) {
            formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,
                    Locale.US);
            return formatter.format(inputDate);
        }
    }
}

Related

  1. format(Date date, String format, Locale locale)
  2. format(Date date, String parttern)
  3. format(Date date, String pattern)
  4. format(Date date, String pattern)
  5. format(Date date, String pattern, Locale locale)
  6. format(double d)
  7. format(Double d)
  8. format(Double number)
  9. format(double number)