Here you can find the source of formatDate(Date date, String pattern)
Parameter | Description |
---|---|
date | a parameter |
pattern | a parameter |
public static String formatDate(Date date, String pattern)
//package com.java2s; /**/*from w w w . j ava 2 s .co m*/ * * Methods Descrip:Converts a line of text into an array of lower case words * using a BreakIterator.wordInstance(). * <p> * * This method is under the Jive Open Source Software License and was * written by Mark Imbriaco. * * @param text * a String of text to convert into an array of words * @return text broken up into an array of words. * */ import java.util.Date; public class Main { /** * * Methods Descrip: * * @param date * @param pattern * @return * */ public static String formatDate(Date date, String pattern) { if (pattern == null || pattern.equals("") || pattern.equals("null")) { pattern = "yyyy-MM-dd"; } java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern); return sdf.format(date); } /** * * Methods Descrip: * * @param timestamp * @param pattern * @return * */ public static String formatDate(long timestamp, String pattern) { Date date = new Date(timestamp); return formatDate(date, pattern); } /** * * Methods Descrip: * * @param timestamp * @return * */ public static String formatDate(String timestamp) { return formatDate(timestamp, ""); } /** * * Methods Descrip: * * @param timestamp * @param pattern * @return * */ public static String formatDate(String timestamp, String pattern) { if (timestamp == null || timestamp.equals("")) { return ""; } try { Long.parseLong(timestamp); } catch (Exception e) { e.printStackTrace(); return null; } Date date = new Date(); return formatDate(date, pattern); } /** * * Methods Descrip: * * @param timestamp * @param pattern * @return * */ public static String formatDate(java.sql.Timestamp timestamp, String pattern) { if (timestamp == null || timestamp.equals("")) { return ""; } Date date = new Date(timestamp.getTime()); return formatDate(date, pattern); } }