Java Date Long Format getLongDisplayDate(Date moment, TimeZone tz, Locale inLocale)

Here you can find the source of getLongDisplayDate(Date moment, TimeZone tz, Locale inLocale)

Description

Get the displayable date and time string for the given moment in time, in the given timezone, localized in long style.

License

Open Source License

Parameter

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.

Return

The localized string for that date and time, in style.

Declaration

public static String getLongDisplayDate(Date moment, TimeZone tz, Locale inLocale) 

Method Source Code

//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 {
    /**/*  w ww. j a  va2 s .  com*/
     * <p>
     * Get the displayable date and time string for the given moment in time, in
     * the given timezone, localized in long style. This method is the same as
     * using {@link #getLongDisplayDate(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#LONG} style.
     */
    public static String getLongDisplayDate(Date moment, TimeZone tz, Locale inLocale) {
        return getLongDisplayDate(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 long 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#LONG} style. Here is an
     * example of date and time in long 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>June 3, 2011 2:14:10 AM EDT</code></dd>
     * <dt>UK English and British Time (<code>Europe/London</code>)</dt>
     * <dd><code>03 June 2011 07:14:10 BST</code></dd>
     * <dt>Germany German and Western Europe Time (<code>America/Berlin</code>)</dt>
     * <dd><code>3. Juni 2011 08:14:10 CEST</code></dd>
     * <dt>Japan Japanese and Japan Time (<code>Asia/Tokyo</code>)</dt>
     * <dd><code>2011/06/03 15:14:10 JST</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#LONG} style.
     */
    public static String getLongDisplayDate(Date moment, TimeZone tz, Locale inLocale, boolean includeTime) {
        return getDisplayDate(moment, tz, inLocale, DateFormat.LONG, 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();
            }
        }
    }
}

Related

  1. getLongDateStr(Date sDate)
  2. getLongDateString(Date date)
  3. getLongDateString(Date date)
  4. getLongDateString(Date date)
  5. getLongDateString(Date date, boolean includeTime)
  6. getLongDisplayTime(long time)
  7. getLongFormatTime(java.util.Date date)
  8. getLongFromDatestamp(String datestamp)
  9. getLongGmtDateString(Date date)