Java TimeZone Get getDisplayDate(Date date, TimeZone tz, Locale inLocale, int style, boolean includeTime)

Here you can find the source of getDisplayDate(Date date, TimeZone tz, Locale inLocale, int style, boolean includeTime)

Description

Private method for getting display date for a particular style: short, medium, long or full.

License

Open Source License

Declaration

private static String getDisplayDate(Date date, TimeZone tz, Locale inLocale, int style, boolean includeTime) 

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 {
    /**//from  w  w w. ja  v a 2 s  .  c  o m
     * 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. getDateInTimeZone(final Date date, final TimeZone timeZone)
  2. getDefaultTimeZoneOffset()
  3. getDefaultTimeZoneRawOffset()
  4. getDefaultTimeZoneString()
  5. getDiffTimeZoneRawOffset(String timeZoneId)
  6. getDSTSavings(TimeZone tz)
  7. getDSTTime(TimeZone tz, Date date)
  8. getEnd(String date, TimeZone tz)
  9. getFilteredTimeZoneMap()