Java String to Date toDateString(Date date, String formatPattern)

Here you can find the source of toDateString(Date date, String formatPattern)

Description

Formats date instance using the provided date format pattern

License

Open Source License

Parameter

Parameter Description
date Date to be formatted
formatPattern Format pattern to apply

Return

Returns the formatted date as a string, or an empty string for null date parameter.

Declaration

public static String toDateString(Date date, String formatPattern) 

Method Source Code

//package com.java2s;
/**/*from   w w w  .  ja va2s. c  o m*/
 * Copyright (c) 2001-2002. Department of Family Medicine, McMaster University. All Rights Reserved.
 * This software is published under the GPL GNU General Public License.
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * This software was written for the
 * Department of Family Medicine
 * McMaster University
 * Hamilton
 * Ontario, Canada
 */

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

    /**
     * Formats date instance using the provided date format pattern
     * 
     * @param date
     *       Date to be formatted
     * @param formatPattern
     *       Format pattern to apply
     * @return
     *       Returns the formatted date as a string, or an empty string for 
     *       null date parameter.
     */
    public static String toDateString(Date date, String formatPattern) {
        if (date == null) {
            return "";
        }

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

    /**
     * Formats the date instance into a string keeping only the date.   
     * 
     * @param date
     *       Date to be formatted using {@link #DEFAULT_DATE_PATTERN}
     * @return
     *       Returns the formatted string
     */
    public static String toDateString(Date date) {
        return toDateString(date, DEFAULT_DATE_PATTERN);
    }
}

Related

  1. toDateString(Date date)
  2. toDateString(Date date)
  3. toDateString(Date date, String format)
  4. toDateString(Date date, String format)
  5. toDateString(Date date, String format)
  6. toDateString(Date date, TimeZone timezone)
  7. toDateString(final Date date)
  8. toDateString(java.util.Date date)
  9. toDateString(java.util.Date date)