Here you can find the source of formatDateTime(Date date)
Parameter | Description |
---|---|
date | the date |
public static String formatDateTime(Date date)
//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()); } }