Java Date to Time toTimeString(Date time)

Here you can find the source of toTimeString(Date time)

Description

Formats the date instance into a string keeping only the time of the day and excluding the remaining info.

License

Open Source License

Parameter

Parameter Description
time Date to be formatted using #DEFAULT_TIME_PATTERN

Return

Returns the formatted string

Declaration

public static String toTimeString(Date time) 

Method Source Code

//package com.java2s;
/**/*from   w ww.ja  v a2s  .c  o m*/
 * Copyright (c) 2001-2002. Department of Family Medicine, McMaster University. All Rights Reserved.
 * This software is published under the GPL GNU General Public License.
 * 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 2
 * 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * This software was written for the
 * Department of Family Medicine
 * McMaster University
 * Hamilton
 * Ontario, Canada
 */

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
    public static final String DEFAULT_TIME_PATTERN = "HH:mm:ss";

    /**
     * Formats the date instance into a string keeping only the time of the day and excluding the remaining info.   
     * 
     * @param time
     *       Date to be formatted using {@link #DEFAULT_TIME_PATTERN}
     * @return
     *       Returns the formatted string
     */
    public static String toTimeString(Date time) {
        return toDateString(time, DEFAULT_TIME_PATTERN);
    }

    /**
     * Formats date instance using the provided date format pattern
     * 
     * @param date
     *       Date to be formatted
     * @param formatPattern
     *       Format pattern to apply
     * @return
     *       Returns the formatted date as a string, or an empty string for 
     *       null date parameter.
     */
    public static String toDateString(Date date, String formatPattern) {
        if (date == null) {
            return "";
        }

        SimpleDateFormat format = new SimpleDateFormat(formatPattern);
        return format.format(date);
    }

    /**
     * Formats the date instance into a string keeping only the date.   
     * 
     * @param date
     *       Date to be formatted using {@link #DEFAULT_DATE_PATTERN}
     * @return
     *       Returns the formatted string
     */
    public static String toDateString(Date date) {
        return toDateString(date, DEFAULT_DATE_PATTERN);
    }
}

Related

  1. ToTimeStr(Date dt)
  2. toTimeString(Date date)
  3. toTimeString(Date date)
  4. toTimeString(Date date)
  5. toTimeString(Date dateTime)
  6. toUIDateTimeString(Date d)
  7. toXSDDateTime(Date date)
  8. toXSDDateTimeString(Date d)