Here you can find the source of format(Date inputDate, String format, Locale locale)
Parameter | Description |
---|---|
inputDate | The date that needs to be formatted. |
format | The given date-time format. |
locale | The given locale. |
Parameter | Description |
---|---|
ParseException | If not a valid date compared to ISO_ZONED_DATE_TIME format |
public static String format(Date inputDate, String format, Locale locale)
//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); } } }