Here you can find the source of dateToString(Date date, String format)
public static String dateToString(Date date, String format)
//package com.java2s; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static final String DATE_PATTERN_SS = "yyyyMMddHHmmssSS"; public static String dateToString(Date date, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); try {/*from ww w . j a v a2s . c o m*/ return sdf.format(date); } catch (Exception e) { return null; } } public static String format(Date date) { SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS); return sdf.format(date); } public static String format(Date date, String pattern) { return date == null ? " " : new SimpleDateFormat(pattern).format(date); } public static String format() { SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_SS); return sdf.format(new Date()); } }