Java Hour Format format(String format, Date val)

Here you can find the source of format(String format, Date val)

Description

Format a Date into a string with the format of the format argument.

License

Open Source License

Parameter

Parameter Description
format The SimpleDateFormat string
val The Date to format

Return

dstr The formatted date string

Declaration

public static String format(String format, Date val) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

import java.util.TimeZone;

public class Main {
    private static SimpleDateFormat stdFormatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    /**/*from  ww  w  . j a v a 2  s . co m*/
     * Format a Date into a string with the format of "yyyyMMddHHmmssSSS".
     * 
     * @param val
     *            The Date to format
     * 
     * @return dstr The formatted date string
     */
    public static String format(Date val) {
        if (val == null)
            return null;
        return stdFormatter.format(val);
    }

    /**
     * Format a Date into a string with the format of the format argument.
     * 
     * @param format
     *            The SimpleDateFormat string
     * @param val
     *            The Date to format
     * 
     * @return dstr The formatted date string
     */
    public static String format(String format, Date val) {
        if (val == null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        formatter.setLenient(false);
        return formatter.format(val);
    }

    public static String format(String format, Date val, Locale locale) {
        if (val == null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat(format, locale);
        formatter.setLenient(false);
        return formatter.format(val);
    }

    /**
     * Format a Date into a string with the format of the format argument for
     * the given timezone.
     * 
     * @param format
     *            The SimpleDateFormat string
     * @param val
     *            The Date to format
     * @param timezone
     *            The timezone
     * 
     * @return dstr The formatted date string
     */
    public static String format(String format, Date val, String timezone) {
        if (val == null)
            return null;
        TimeZone tz = null;
        if (timezone == null || timezone.length() == 0) {
            tz = TimeZone.getDefault();
        } else {
            tz = TimeZone.getTimeZone(timezone);
        }
        return format(format, val, tz);
    }

    /**
     * This private format method is called by the public format method that
     * accepts a timezone.
     * 
     * @param format
     *            The SimpleDateFormat string
     * @param val
     *            The Date to format
     * @param timezone
     *            The timezone
     * 
     * @return dstr The formatted date string
     */
    private static String format(String format, Date val, TimeZone timezone) {
        if (val == null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        formatter.setTimeZone(timezone);
        formatter.setLenient(false);
        return formatter.format(val);
    }
}

Related

  1. format(java.util.Date date, String format)
  2. format(List list, int scale)
  3. format(long time)
  4. format(long time)
  5. format(Number price)
  6. format(String pattern)
  7. format(String str)
  8. format16ByDate(Date aDate)
  9. format1StringToformat2String(String dateStr)