Java Date Now getCurrentDate(String pattern)

Here you can find the source of getCurrentDate(String pattern)

Description

Formats the current date into a string according to a specified pattern.

License

Open Source License

Parameter

Parameter Description
pattern The SimpleDateFormat pattern to use (e.g. "yyyyMMddHHmmss").

Return

The current formatted date string.

Declaration

public static String getCurrentDate(String pattern) 

Method Source Code

//package com.java2s;
/*//  w w  w  .j ava 2  s. com
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */

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

public class Main {
    /**
     * Formats the current date into a string according to a specified pattern.
     * 
     * @param pattern
     *            The SimpleDateFormat pattern to use (e.g. "yyyyMMddHHmmss").
     * @return The current formatted date string.
     */
    public static String getCurrentDate(String pattern) {
        return formatDate(pattern, new Date());
    }

    /**
     * Formats a java.util.Date object into a string according to a specified pattern.
     * 
     * @param pattern
     *            The SimpleDateFormat pattern to use (e.g. "yyyyMMddHHmmss").
     * @param date
     *            The java.util.Date object to format.
     * @return The formatted date string.
     */
    public static String formatDate(String pattern, Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }
}

Related

  1. getCurrentDate(String format)
  2. getCurrentDate(String format)
  3. getCurrentDate(String formatStr)
  4. getCurrentDate(String pattern)
  5. getCurrentDate(String pattern)
  6. getCurrentDate(String pattern, Locale locale)
  7. getCurrentDate2(String format)
  8. getCurrentDate_String()
  9. getCurrentDate_YYYY_MM_DD()