Java Date Parse asDate(Date date, String format)

Here you can find the source of asDate(Date date, String format)

Description

Formats a Date based on the provided format.

License

Open Source License

Parameter

Parameter Description
date a parameter
format a parameter

Declaration

public static String asDate(Date date, String format) 

Method Source Code


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

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    /**//w  w  w  .ja va 2s .  c  o m
     * Formats a Date based on the provided format. If the date is null, 'Unknown' will be returned
     * 
     * @param date
     * @param format
     * @return
     */
    public static String asDate(Date date, String format) {
        return asDate(date, format, "Unknown");
    }

    /**
     * Formats a Date based on the provided format. If the date is null, the default value will be returned
     * 
     * @param date
     * @param format
     * @return
     */

    public static String asDate(Date date, String format, String dft) {
        if (date == null)
            return dft;

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
}

Related

  1. asDate(final long time)
  2. asDate(final Map map, final String key)
  3. asDate(final String literal)
  4. asDate(String date)