Java Date to Time getMediumDateString(Date date, boolean includeTime)

Here you can find the source of getMediumDateString(Date date, boolean includeTime)

Description

Returns medium date string representation in localized format (e.g.

License

Open Source License

Parameter

Parameter Description
date Date to convert into string
includeTime Indicates whether to append time string or not. If true, time string will be appended in short format (e.g. 11:01)

Declaration

public static String getMediumDateString(Date date, boolean includeTime) 

Method Source Code


//package com.java2s;
/*// w w  w.j a  v  a2  s.c  om
 * $Id: DateUtils.java,v 1.4 2005/10/10 18:02:45 rbair Exp $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.text.DateFormat;

import java.util.Date;

public class Main {
    /**
     * Returns medium date string representation in localized format (e.g.
     * 02/11/2012).
     *
     * @param date Date to convert into string
     * @param includeTime Indicates whether to append time string or not. If
     * true, time string will be appended in short format (e.g. 11:01)
     */
    public static String getMediumDateString(Date date, boolean includeTime) {
        if (includeTime) {
            return getDateTimeString(date, DateFormat.MEDIUM, DateFormat.SHORT);
        } else {
            return getDateString(date, DateFormat.MEDIUM);
        }
    }

    /**
     * Returns date and time string representation in localized format.
     *
     * @param date Date to convert into string
     * @param dateStyle Date style format (DateFormat.SHORT, DateFormat.MEDIUM,
     * DateFormat.LONG)
     * @param timeStyle Time style format (DateFormat.SHORT, DateFormat.MEDIUM,
     * DateFormat.LONG)
     */
    public static String getDateTimeString(Date date, int dateStyle, int timeStyle) {
        if (date == null) {
            return "";
        }
        DateFormat f = DateFormat.getDateTimeInstance(dateStyle, timeStyle);
        return f.format(date);
    }

    /**
     * Returns date string representation in localized format.
     *
     * @param date Date to convert into string
     * @param dateStyle Date style format (DateFormat.SHORT, DateFormat.MEDIUM,
     * DateFormat.LONG)
     */
    public static String getDateString(Date date, int dateStyle) {
        if (date == null) {
            return "";
        }
        DateFormat f = DateFormat.getDateInstance(dateStyle);
        return f.format(date);
    }
}

Related

  1. extractTimeFromDate(Date date)
  2. getFriendlyTime(Date dateTime)
  3. getFriendlyTimeString(Date date)
  4. getMSDateTimeString(Date date)
  5. getTime(Date date)
  6. getTime(Date date)
  7. getTime(Date date)