Here you can find the source of toDateString(Date date, String formatPattern)
Parameter | Description |
---|---|
date | Date to be formatted |
formatPattern | Format pattern to apply |
public static String toDateString(Date date, String formatPattern)
//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); } }