Java Timestamp Format formatDateTime(Date date)

Here you can find the source of formatDateTime(Date date)

Description

Format date time.

License

Open Source License

Parameter

Parameter Description
date the date

Return

the string

Declaration

public static String formatDateTime(Date date) 

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 ww  . j a v  a  2  s  .c o m*/
     * Format date time.
     * 
     * @param date
     *            the date
     * 
     * @return the string
     */
    public static String formatDateTime(Date date) {
        return (formatDate(date, "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * Format date time.
     * 
     * @return the string
     */
    public static String formatDateTime() {
        return (formatDate(now(), "MM-dd-yyyy HH:mm:ss"));
    }

    /**
     * Format date time.
     * 
     * @param o
     *            the o
     * 
     * @return the string
     */
    public static String formatDateTime(Object o) {
        if ((o == null) || (o.toString().trim().length() == 0)) {
            return "";
        }
        if (o.getClass() == String.class) {
            return formatDateTime((String) o);
        } else if (o.getClass() == Date.class) {
            return formatDateTime((Date) o);
        } else if (o.getClass() == Timestamp.class) {
            return formatDateTime(new Date(((Timestamp) o).getTime()));
        } else {
            return o.toString();
        }
    }

    /**
     * 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. formatDate(Timestamp date)
  2. formatDate(Timestamp time)
  3. formatDate(Timestamp time, String pattern)
  4. formatDate(Timestamp timestamp)
  5. formatDateHour(Date date)
  6. formatDateTime(java.sql.Timestamp ts)
  7. formatDateTime(long ms)
  8. formatDateTime(String dTime)
  9. formatDateTime(Timestamp t, String pattern)