Java Hour Format formatDate(Date d, String pattern, TimeZone tz)

Here you can find the source of formatDate(Date d, String pattern, TimeZone tz)

Description

format Date

License

BSD License

Declaration

public static String formatDate(Date d, String pattern, TimeZone tz) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright SemanticBits, Northwestern University and Akaza Research
 * //from w  w w.ja v a 2  s  .  co m
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/caaers/LICENSE.txt for details.
 ******************************************************************************/

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static final String DATE_PATTERN = "MM/dd/yyyy";
    public static final String DATE_WITH_DATETIME = "MM/dd/yyyy HH:mm";

    public static String formatDate(Date d) {
        if (d.getHours() > 0 || d.getMinutes() > 0)
            return formatDate(d, DATE_WITH_DATETIME);
        return formatDate(d, DATE_PATTERN);
    }

    public static String formatDate(Date d, String pattern) {
        return formatDate(d, pattern, TimeZone.getDefault());
    }

    public static String formatDate(Date d, String pattern, TimeZone tz) {
        //BJ: date formats are not thread safe.
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        df.setTimeZone(tz);
        return df.format(d);
    }
}

Related

  1. formatCurrentTime()
  2. formatCurrentTime()
  3. formatCurrentTime(final String format)
  4. formatDate(Calendar currentDate, String pattern)
  5. formatDate(Calendar time)
  6. formatDate(Date d, TimeZone tz)
  7. formatDate(Date date, boolean includeTime)
  8. formatDate(Date date, boolean time, boolean csv)
  9. formatDate(Date date, boolean withTime)