Here you can find the source of format(String format, Date val)
Parameter | Description |
---|---|
format | The SimpleDateFormat string |
val | The Date to format |
public static String format(String format, Date val)
//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); } }