Java yyyy formatDate(Date date, String datePattern)

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

Description

Format date with specified pattern yyyy-MM-dd

License

Open Source License

Parameter

Parameter Description
date specified date
datePattern specified date pattern

Return

string of date with @param datePattern

Declaration

public static String formatDate(Date date, String datePattern) 

Method Source Code


//package com.java2s;
/*/* w w  w  .  j a  v  a2  s  . c  om*/
 * File: $RCSfile$
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */

import java.util.Date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Main {
    /**
     * Format date with specified pattern yyyy-MM-dd
     *
     * @param date        specified date
     * @param datePattern specified date pattern
     * @return string of date with @param datePattern
     */
    public static String formatDate(Date date, String datePattern) {
        String ret = null;
        try {
            DateFormat dateFormat = new SimpleDateFormat(datePattern);
            ret = dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * Format date with default pattern yyyy-MM-dd
     *
     * @param date specified date
     * @return string of date with default pattern
     */
    public static String formatDate(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }
}

Related

  1. formatAsCassandraDate(Date date)
  2. formatAsNumber(Date date)
  3. formatBySlashForm(Date aDate)
  4. formatDate(Calendar cal, String pattern)
  5. formatDate(Calendar calendar, String format)
  6. formatDate(Date date, String format)
  7. formatDate(Date date, String format)
  8. formatDate(Date date, String outputPattern)
  9. formatDate(Date date, String pattern)