Here you can find the source of dateToString(Date date)
public static String dateToString(Date date)
//package com.java2s; //License from project: Apache License import java.text.SimpleDateFormat; public class Main { public static String dateToString(Date date) { if (date == null) return ""; return FormatDate(date, "yyyy-MM-dd HH:mm:ss"); }// w w w . ja v a 2 s. co m public static String dateToString(Date date, String sf) { if (date == null) return ""; return FormatDate(date, sf); } public static String FormatDate(Date date, String sf) { if (date == null) return ""; SimpleDateFormat dateformat = new SimpleDateFormat(sf); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if (date.getHours() == 0) return sdf.format(date); else return dateformat.format(date); } }