Java yyyy formatYmd(Date d)

Here you can find the source of formatYmd(Date d)

Description

Utility method to format a date in yyyy-MM-dd format

License

Open Source License

Parameter

Parameter Description
d the date to format

Return

a String representing the date in the passed format

Declaration

public static String formatYmd(Date d) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    /**/*from w w  w  .  j a va2 s  .c o  m*/
     * Utility method to format a date in yyyy-MM-dd format
     * @param d the date to format
     * @return a String representing the date in the passed format
     */
    public static String formatYmd(Date d) {
        return formatDate(d, "yyyy-MM-dd", "");
    }

    /**
     * Utility method to format a date in the given format
     * @param d the date to format
     * @return a String representing the date in the passed format
     */
    public static String formatDate(Date d, String format) {
        return formatDate(d, format, "");
    }

    /**
     * Utility method to format a date in the given format
     * @param d the date to format
     * @param format the DateFormat to use
     * @param defaultIfNUll the value to return if the passed date is null
     * @return a String representing the date in the passed format
     */
    public static String formatDate(Date d, String format,
            String defaultIfNull) {
        if (d != null) {
            DateFormat df = new SimpleDateFormat(format);
            return df.format(d);
        }
        return defaultIfNull;
    }
}

Related

  1. formatTimeSpan(long time)
  2. formatToEapoDate(Date date)
  3. formatToSqlDate(final Date date)
  4. formatToString(String format, long time)
  5. formatYMD(Date d)