Here you can find the source of dateTimeFormat(Date date,String pattern)
public static String dateTimeFormat(Date date,String pattern) throws ParseException
//package com.java2s; //License from project: Apache License import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Date; public class Main { public static String dateTimeFormat(Date date, String pattern) throws ParseException { String s = ""; try {//w ww. java 2 s. c o m SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); s = dformat.format(date); s = String.valueOf(StrToTimestamp(s, pattern)); s = s.substring(0, pattern.length()); } catch (Exception e) { } return s; } public static String dateTimeFormat(Date date) throws ParseException { String s = ""; try { SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd"); s = dformat.format(date); s = String.valueOf(StrToTimestamp(s, "yyyy-MM-dd")); s = s.substring(0, "yyyy-MM-dd".length()); } catch (Exception e) { } return s; } public static Timestamp StrToTimestamp(String timestampStr, String pattern) throws ParseException { java.util.Date date = null; SimpleDateFormat format = new SimpleDateFormat(pattern); try { date = format.parse(timestampStr); } catch (ParseException ex) { throw ex; } return date == null ? null : new Timestamp(date.getTime()); } }