Java Locale Format formatDate(Date date, String pattern)

Here you can find the source of formatDate(Date date, String pattern)

Description

The date of Date type conversion, in accordance with the specified format to a String.

License

Open Source License

Parameter

Parameter Description
date target date
pattern formate pattern

Return

String, return value will be null if has exceptions.

Declaration

public static String formatDate(Date date, String pattern) 

Method Source Code


//package com.java2s;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class Main {
    /**//from w  w w. ja v  a 2  s  .c o  m
     * The date of Date type conversion, in accordance with the specified format to a String.
     * 
     * <pre>
     * Locale = Locale.US.
     * examples:
     * <code>
     * DateUtils.formatDateWithPattern(new Date(0), "yyyyMMdd") = "19700101"
     * </code>
     * </pre>
     * 
     * @param date target date
     * @param pattern formate pattern
     * @return String, return value will be <code>null</code> if has exceptions.
     */
    public static String formatDate(Date date, String pattern) {
        String result = null;
        if (date == null || pattern == null) {
            return result;
        }
        try {
            result = getDateFormat(pattern).format(date);
        } catch (Exception e) {
            result = null;
        }

        return result;
    }

    /**
     * return date format with Locale US.
     * 
     * @param pattern date format pattern.
     * @return DateFormat
     */
    public static DateFormat getDateFormat(String pattern) {
        return new SimpleDateFormat(pattern, Locale.US);
    }
}

Related

  1. formatComma(String number)
  2. formatDate(Calendar cal, String format)
  3. formatDate(Date date, String format)
  4. formatDate(Date date, String format)
  5. formatDate(Date date, String format, String localeText)
  6. formatDate(Date date, String pattern)
  7. formatDate(Date date, String pattern, Locale locale)
  8. formatDate(Date date, String pPatern)
  9. formatDate(Date value, String pattern, TimeZone tz)