Java Time Format getTimeAsString(final Date date, final String format)

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

Description

Returns the given Date as a formatted String for the given formatting rule.

License

Open Source License

Parameter

Parameter Description
date The Date to format
format The formatting rule

Return

String Date as formatted String

Declaration

public static final String getTimeAsString(final Date date, final String format) 

Method Source Code

//package com.java2s;
/** //  w w  w .ja  va  2 s. c  o  m
 * ============================================================================
 * Xirp 2: eXtendable interface for robotic purposes.
 * ============================================================================
 * 
 * Copyright (C) 2005-2007, by Authors and Contributors listed in CREDITS.txt
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at:
 *
 *             http://www.opensource.org/licenses/cpl1.0.php
 *
 * ----------------------------
 * Util.java
 * ----------------------------
 *
 * Original Author:  Matthias Gernand [matthias.gernand AT gmx.de]
 * Contributor(s):   Rabea Gransberger [rgransberger AT web.de]
 *
 * Changes
 * -------
 * 08.05.2006:      Created by Matthias Gernand.
 */

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

public class Main {
    /**
     * Returns the given date as a formatted String. It is formatted
     * like this:<br>
     * 2006-05-08_16-12-56<br>
     * yyyy-MM-dd_HH-mm-ss
     * 
     * @param date
     *            The date to format
     * @return Date as formatted string
     */
    public static final String getTimeAsString(final Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); //$NON-NLS-1$
        return format.format(date);
    }

    /**
     * Returns the given Date as a formatted String for the given
     * formatting rule.
     * 
     * @param date
     *            The Date to format
     * @param format
     *            The formatting rule
     * @return String Date as formatted String
     */
    public static final String getTimeAsString(final Date date, final String format) {
        SimpleDateFormat frmt = new SimpleDateFormat(format);
        return frmt.format(date);
    }

    /**
     * Returns the given time in milliseconds as a formatted String.
     * It is formatted like this:<br>
     * 2006-05-08_16-12-56<br>
     * yyyy-MM-dd_HH-mm-ss
     * 
     * @param millis
     *            The milliseconds to format
     * @return String time as formatted String
     */
    public static final String getTimeAsString(final long millis) {
        return getTimeAsString(new Date(millis));
    }

    /**
     * Returns the given time in milliseconds as a formatted String
     * for the given formatting rule.
     * 
     * @param millis
     *            The time in milliseconds to format
     * @param format
     *            The formatting rule
     * @return String Date as formatted String
     */
    public static final String getTimeAsString(final long millis, final String format) {
        SimpleDateFormat frmt = new SimpleDateFormat(format);
        return frmt.format(new Date(millis));
    }
}

Related

  1. getTime(String dateStr, String formate)
  2. getTime(String format)
  3. getTime(String format, String date)
  4. getTime(String timeFormat)
  5. getTimeAfter(int field, int amount, String formatStr)
  6. getTimeAsString(String timeFormatAsString)
  7. getTimeByDate(Date date, String timeFormat)
  8. getTimeByFormat(Date date, String format)
  9. getTimeCount(String from, String to, String format)