Java Date to String dateToString(Date date, String format)

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

Description

Creates a String presentation of the given date according to the given format.

License

Open Source License

Parameter

Parameter Description
date date to be presented as String
format date format

Return

date string

Declaration

public static String dateToString(Date date, String format) 

Method Source Code

//package com.java2s;
/**// ww  w .j  a v a  2s .  c  o  m
 * This file is part of Location Service :: Admin. Copyright (C) 2014 Petteri
 * Kivim?ki
 *
 * Location Service :: Admin 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.
 *
 * Location Service :: Admin 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
 * Location Service :: Admin. If not, see <http://www.gnu.org/licenses/>.
 */

import java.text.DateFormat;

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

public class Main {
    /**
     * Creates a String presentation of the given date in the format "yyyy-MM-dd
     * HH:mm:ss".
     *
     * @param date date to be presented as String
     * @return date string
     */
    public static String dateToString(Date date) {
        String format = "yyyy-MM-dd HH:mm:ss";
        return dateToString(date, format);
    }

    /**
     * Creates a String presentation of the given date according to the given
     * format.
     *
     * @param date date to be presented as String
     * @param format date format
     * @return date string
     */
    public static String dateToString(Date date, String format) {
        /* Format the date according to the given format. */
        DateFormat formatter;
        formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }
}

Related

  1. dateToString(Date date)
  2. dateToString(Date date)
  3. dateToString(Date date, String dateFormat)
  4. dateToString(Date date, String formartStr)
  5. dateToString(Date date, String format)
  6. dateToString(Date date, String format)
  7. dateToString(Date date, String format)
  8. dateToString(Date date, String format)
  9. dateToString(Date date, String format)