Here you can find the source of formatDateToStr(String format, Date date)
public static String formatDateToStr(String format, Date date)
//package com.java2s; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static final String yyyyMMdd = "yyyyMMdd"; public static final String yyyy_MM_dd = "yyyy-MM-dd"; public static final String ddMMyy = "ddMMyy"; public static final String yyyyMM = "yyyyMM"; public static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss"; public static final String yyMMddHHmmss = "yyMMddHHmmss"; public static final String yyyyMMddHH = "yyyyMMddHH"; public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss"; public static final String HH_mm_ss = "HH:mm:ss"; public static final String HHmmss = "HHmmss"; public static final SimpleDateFormat dateFormat = new SimpleDateFormat(yyyyMMdd); public static final SimpleDateFormat dateFormat2 = new SimpleDateFormat(yyyy_MM_dd); public static final SimpleDateFormat dateFormat3 = new SimpleDateFormat(ddMMyy); public static final SimpleDateFormat dateFormat4 = new SimpleDateFormat(yyyyMM); public static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss); public static final SimpleDateFormat dateTimeFormat2 = new SimpleDateFormat(yyMMddHHmmss); public static final SimpleDateFormat dateTimeFormat3 = new SimpleDateFormat(yyyyMMddHH); public static final SimpleDateFormat dateTimeFormat4 = new SimpleDateFormat(yyyyMMddHHmmss); public static final SimpleDateFormat timeFormat = new SimpleDateFormat(HH_mm_ss); public static final SimpleDateFormat timeFormat2 = new SimpleDateFormat(HHmmss); public static String formatDateToStr(String format, Date date) { String dateStr = null;//www .ja v a 2s. c o m if (yyyyMMdd.equals(format)) { dateStr = dateFormat.format(date); } else if (yyyy_MM_dd.equals(format)) { dateStr = dateFormat2.format(date); } else if (ddMMyy.equals(format)) { dateStr = dateFormat3.format(date); } else if (yyyyMM.equals(format)) { dateStr = dateFormat4.format(date); } else if (yyyy_MM_dd_HH_mm_ss.equals(format)) { dateStr = dateTimeFormat.format(date); } else if (yyMMddHHmmss.equals(format)) { dateStr = dateTimeFormat2.format(date); } else if (yyyyMMddHH.equals(format)) { dateStr = dateTimeFormat3.format(date); } else if (yyyyMMddHHmmss.equals(format)) { dateStr = dateTimeFormat4.format(date); } else if (HH_mm_ss.equals(format)) { dateStr = timeFormat.format(date); } else if (HHmmss.equals(format)) { dateStr = timeFormat2.format(date); } return dateStr; } }