Java Timestamp Format formatDate(Date date, String pattern)

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

Description

Format date.

License

Open Source License

Parameter

Parameter Description
date the date
pattern the pattern

Return

the string

Declaration

public static String formatDate(Date date, String pattern) 

Method Source Code


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

Related

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