Java Locale Format formatDate(java.util.Date d, String format)

Here you can find the source of formatDate(java.util.Date d, String format)

Description

Format date using a mask and the default locale

License

LGPL

Parameter

Parameter Description
d Date object
format Date mask, like yyyy-MM-dd or any valid java format

Exception

Parameter Description
Throwable an exception

Return

String representing the formatted string

Declaration

public static String formatDate(java.util.Date d, String format) throws Throwable 

Method Source Code

//package com.java2s;
/**/*from w  w  w .jav a2 s.  co m*/
 * Core-level framework class: String and Date basic utility methods.
 * <br><br>
 * Encapsulates utility methods for everyday programming tasks
 * with Strings, Dates and other common stuff.
 * <br>
 * Creation date: 18/09/2003<br>
 * Last Update: 18/09/2003<br>
 * (c) 2003 Martin Cordova<br>
 * This code is released under the LGPL license<br>
 * @author Martin Cordova (some code written by Carlos Pineda)
 */

import java.text.SimpleDateFormat;

import java.util.Locale;
import java.util.Map;

public class Main {
    /**
     * Format date using a mask and the default locale
     * @param d Date object
     * @param format Date mask, like yyyy-MM-dd or any valid java format
     * @return String representing the formatted string
     * @throws Throwable
     */
    public static String formatDate(java.util.Date d, String format) throws Throwable {
        SimpleDateFormat f = new SimpleDateFormat();
        f.applyPattern(format);
        return f.format(d);
    }

    /**
     * Format date using a mask and locale
     * @param d Date object
     * @param format Date mask, like yyyy-MM-dd or any valid java format
     * @param loc Custom Locale
     * @return String representing the formatted string
     * @throws Throwable
     */
    public static String formatDate(java.util.Date d, String format, Locale loc) throws Throwable {
        SimpleDateFormat f = new SimpleDateFormat(format, loc);
        return f.format(d);
    }

    public static String format(String pattern, Map<String, Object> arguments) {
        String formatedStr = pattern;
        for (String key : arguments.keySet()) {
            formatedStr = formatedStr.replaceAll("\\{:" + key + "\\}", arguments.get(key).toString());
        }
        return formatedStr;
    }
}

Related

  1. formatDate(final Date dat, final String pattern)
  2. formatDate(final Date date, final String datepattern, TimeZone tz)
  3. formatDate(final Date date, final String format)
  4. formatDate(final Date date, final String pattren)
  5. formatDate(java.util.Date d, String format)
  6. formatDate(java.util.Date date, String formatPattern)
  7. formatDate(String _Date)
  8. formatDate(String dateStr, String format, String toFormat)
  9. formatDate(String pattern, Date date, String valueForNull)