Here you can find the source of formatDate(Date date, String pattern)
Parameter | Description |
---|---|
date | target date |
pattern | formate pattern |
null
if has exceptions.
public static String formatDate(Date date, String pattern)
//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); } }