Here you can find the source of getMediumDisplayDate(Date moment, TimeZone tz, Locale inLocale)
Get the displayable date and time string for the given moment in time, in the given timezone, localized in medium style.
Parameter | Description |
---|---|
moment | A moment in time. |
tz | A timezone in which to format the date and time for that moment. |
inLocale | A locale in which to format the date and time for that moment. |
public static String getMediumDisplayDate(Date moment, TimeZone tz, Locale inLocale)
//package com.java2s; import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Locale; import java.util.TimeZone; public class Main { /**/*from w w w . j a va 2 s . co m*/ * <p> * Get the displayable date and time string for the given moment in time, in * the given timezone, localized in medium style. This method is the same as * using {@link #getMediumDisplayDate(Date, TimeZone, Locale, boolean)} * where the boolean flag is true. * </p> * * @param moment * A moment in time. * @param tz * A timezone in which to format the date and time for that * moment. * @param inLocale * A locale in which to format the date and time for that moment. * @return The localized string for that date and time, in * {@link java.util.DateFormat#MEDIUM} style. */ public static String getMediumDisplayDate(Date moment, TimeZone tz, Locale inLocale) { return getMediumDisplayDate(moment, tz, inLocale, true); } /** * <p> * Get the displayable date (and optionally time) string for the given * moment in time, in the given timezone, localized in medium style. The * boolean flag controls whether time is included in the returned string ( * <code>true</code>), or only date (year, month and day) ( * <code>false</code>). * </p> * <p> * This uses the localization patterns built into the JVM for the * {@link java.util.DateFormat} and {@link java.util.SimpleDateFormat} * classes, and the {@link java.util.DateFormat#MEDIUM} style. Here is an * example of date and time in medium style, for June 3, 2011, at 6:14:10 AM * GMT, for 4 locales and their typical timezones. (Only the date part would * be returned if that was what was requested.) * </p> * <dl> * <dt>US English and US Eastern Time (<code>America/New_York</code>)</dt> * <dd><code>Jun 3, 2011 2:14:10 AM</code></dd> * <dt>UK English and British Time (<code>Europe/London</code>)</dt> * <dd><code>03-Jun-2011 07:14:10</code></dd> * <dt>Germany German and Western Europe Time (<code>America/Berlin</code>)</dt> * <dd><code>03.06.2011 08:14:10</code></dd> * <dt>Japan Japanese and Japan Time (<code>Asia/Tokyo</code>)</dt> * <dd><code>2011/06/03 15:14:10</code></dd> * </dl> * <p> * If you give a null timezone and ask for time to be included, GMT will be * assumed. If you give a null timezone, and do not ask for time to be * included, then no timezone will be assumed. (Note this means that the JVM * internally will revert to server timezone.) If you pass null to any other * parameter, then null will be returned. * </p> * * @param moment * A moment in time. * @param tz * A timezone in which to format the date and time for that * moment. * @param inLocale * A locale in which to format the date and time for that moment. * @param includeTime * A control flag: return both date and time (<code>true</code>) * or date only (<code>false</code>) * @return The localized string for that date and time, in * {@link java.util.DateFormat#MEDIUM} style. */ public static String getMediumDisplayDate(Date moment, TimeZone tz, Locale inLocale, boolean includeTime) { return getDisplayDate(moment, tz, inLocale, DateFormat.MEDIUM, includeTime); } /** * Private method for getting display date for a particular style: short, * medium, long or full. */ private static String getDisplayDate(Date date, TimeZone tz, Locale inLocale, int style, boolean includeTime) { if (date == null || inLocale == null) { return null; } // if not given a timezone but time is desired, default to GMT; // otherwise no default (SimpleDateFormat will revert to server timezone // in that case) if (includeTime && (tz == null)) { tz = TimeZone.getTimeZone("GMT"); } try { SimpleDateFormat formatter; if (includeTime) { formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(style, style, inLocale); } else { formatter = (SimpleDateFormat) DateFormat.getDateInstance(style, inLocale); } if (tz != null) { formatter.setTimeZone(tz); } return formatter.format(date); } catch (Exception e1) { // should never happen try { SimpleDateFormat formatter; if (includeTime) { formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(style, style, Locale.getDefault()); } else { formatter = (SimpleDateFormat) DateFormat.getDateInstance(style, Locale.getDefault()); } return formatter.format(date); } catch (Exception e2) { // should really never happen return date.toString(); } } } }