Java Second Convert toTimeStringNoSeconds(Date timeString)

Here you can find the source of toTimeStringNoSeconds(Date timeString)

Description

to Time String No Seconds

License

Open Source License

Declaration

public static String toTimeStringNoSeconds(Date timeString) 

Method Source Code

//package com.java2s;
/**// w w w . j  a  v  a 2s  .  c  om
 * 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 String toTimeStringNoSeconds(Date timeString) {
        return toDateString(timeString, "HH:mm");
    }

    /**
     * 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. toSeconds(long millis)
  2. toSeconds(long milliseconds)
  3. toSeconds(long nanoseconds)
  4. toSeconds(long nanoSeconds)
  5. toSecondsPrecisionInMs(long ms)
  6. unixtimeToString(long unixSeconds)