Here you can find the source of toYMD(Date date, String datePtn)
public static String toYMD(Date date, String datePtn)
//package com.java2s; import java.sql.Date; import java.sql.Time; import java.text.*; public class Main { public static final String DEFAULT_DATE_YMD_FORMAT = "yyyy-MM-dd"; public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss"; public static String toYMD(Date date, String datePtn) { if (date == null) { return null; }//w w w . j a v a 2 s . co m return toString(date, getYMDFormat(datePtn)); } public static String toYMD(String yy, String mm, String dd) { if (yy == null || mm == null || dd == null) { return null; } if (yy.trim().length() == 0 || mm.trim().length() == 0) { return ""; } mm = mm != null && mm.length() == 1 ? "0" + mm : mm; if (dd != null && dd.length() == 0) { dd = " "; } if (dd != null && dd.length() == 1) { dd = "0" + dd; } return yy + mm + dd; } public static String toString(Date date) { return toString((java.util.Date) date); } public static String toString(java.util.Date date) { return toString(date, DEFAULT_DATE_YMD_FORMAT); } public static String toString(Date date, String format) { return toString((java.util.Date) date, format); } public static String toString(java.util.Date date, String format) { if (date == null) { return null; } if (format == null) { throw new IllegalArgumentException("The value of an argument is inaccurate."); } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } public static String toString(Time time, String format) { if (time == null) { return null; } if (format == null) { throw new IllegalArgumentException("The value of an argument is inaccurate."); } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(time); } public static String toString(Time time) { return toString(time, DEFAULT_TIME_FORMAT); } public static String getYMDFormat(String datePtn) { final String[][] DATE_FORMAT_YMD_LIST = { { "1", "yyyy/MM/dd" }, { "2", "yyyy.MM.dd" }, { "3", "yyyy-MM-dd" }, { "4", "MM/dd/yyyy" }, { "5", "MM.dd.yyyy" }, { "6", "MM-dd-yyyy" }, { "7", "dd/MM/yyyy" }, { "8", "dd.MM.yyyy" }, { "9", "dd-MM-yyyy" }, { "A", "dd/MM yyyy" } }; String format = null; for (int i = 0; i < DATE_FORMAT_YMD_LIST.length; i++) { if (DATE_FORMAT_YMD_LIST[i][0].equals(datePtn)) { format = DATE_FORMAT_YMD_LIST[i][1]; break; } } if (format == null) { throw new IllegalArgumentException("The value of an argument is inaccurate."); } return format; } }