Android Date Format formatDate(Date date, String pattern)

Here you can find the source of formatDate(Date date, String pattern)

Description

Formats a date into a date/time string using the default TimeZone for this host.

License

Open Source License

Parameter

Parameter Description
date the date to be formatted
pattern the pattern describing the date and time format

Return

the formatted time string

Declaration

public static String formatDate(Date date, String pattern) 

Method Source Code

//package com.java2s;
/*/*w  ww . j a v  a  2 s  .com*/
 * Copyright (C) 2011-2012 Inaki Ortiz de Landaluce Saiz
 * 
 * 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 3 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 Lesser General Public 
 * License along with this program. If not, see 
 * <http://www.gnu.org/licenses/>
 */

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    /**
     * Formats a date into a date/time string using the default TimeZone for
     * this host.
     * 
     * @param date
     *            the date to be formatted
     * @param pattern
     *            the pattern describing the date and time format
     * @return the formatted time string
     */
    public static String formatDate(Date date, String pattern) {
        return formatDate(date, pattern, TimeZone.getDefault());
    }

    /**
     * Formats a date into a date/time string
     * 
     * @param date
     *            the date to be formatted
     * @param pattern
     *            the pattern describing the date and time format
     * @param zone
     *            the time zone
     * @return the formatted time string
     */
    public static String formatDate(Date date, String pattern, TimeZone zone) {
        DateFormat dfmt = new SimpleDateFormat(pattern);
        dfmt.setTimeZone(zone);
        return dfmt.format(date);
    }
}

Related

  1. formatDate(Date date)
  2. formatDate(Date date)
  3. formatDate(Date date, String dateFormat)
  4. formatDate(Date date, String dateFormat, Locale locale)
  5. formatDate(Date date, String pattern)
  6. formatDate(Date date, String pattern)
  7. formatDate(Date date, String pattern)
  8. formatDate(Date date, String pattern, TimeZone zone)
  9. formatDate(Date time)