Java Timestamp Format formatDate(Date date, String pattern)

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

Description

Methods Descrip:

License

LGPL

Parameter

Parameter Description
date a parameter
pattern a parameter

Declaration

public static String formatDate(Date date, String pattern) 

Method Source Code

//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);
    }
}

Related

  1. format8chars(long timestamp)
  2. formataDataHoraMinuto(Timestamp dataHora)
  3. formatCalendar(Timestamp stamp)
  4. formatCurrentDateTimeStamp(final String dateFormatString)
  5. formatDataTime(Timestamp intime)
  6. formatDate(Date date, String pattern)
  7. formatDate(java.sql.Timestamp timestamp)
  8. formatDate(java.util.Date d)
  9. formatDate(java.util.Date date)