Here you can find the source of formatDate(Date date, String pattern)
Parameter | Description |
---|---|
date | the date |
pattern | the pattern |
public static String formatDate(Date date, String pattern)
//package com.java2s; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Main { /** The en locale. */ private static Locale enLocale = new Locale("en", "US"); /**// w w w .jav a 2 s. c om * Format date. * * @param date * the date * @param pattern * the pattern * * @return the string */ public static String formatDate(Date date, String pattern) { if (date == null) { return ""; } if (pattern == null) { pattern = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(pattern, enLocale); return sdf.format(date); } /** * Format date. * * @param date * the date * * @return the string */ public static String formatDate(Date date) { return (formatDate(date, "yyyy-MM-dd")); } /** * Format date. * * @return the string */ public static String formatDate() { return (formatDate(now(), "yyyy-MM-dd")); } /** * Format date. * * @param o * the o * * @return the string */ public static String formatDate(Object o) { if (o == null) { return ""; } if (o.getClass() == String.class) { return formatDate((String) o); } else if (o.getClass() == Date.class) { return formatDate((Date) o); } else if (o.getClass() == Timestamp.class) { return formatDate(new Date(((Timestamp) o).getTime())); } else { return o.toString(); } } /** * Now. * * @return the date */ public static Date now() { return (new Date()); } }