Here you can find the source of formatDateTime(Date myDate)
public static String formatDateTime(Date myDate)
//package com.java2s; //License from project: Apache License import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class Main { private static final String patternDate = "yyyy-MM-dd"; private static final String patternDateTime = "yyyy-MM-dd HH:mm:ss"; public static String formatDateTime(Date myDate, String pattern) { myDate = isDate(myDate);// w w w .j a va2s . co m // SimpleDateFormat fd = new SimpleDateFormat(pattern, Locale.CHINA); SimpleDateFormat fd = new SimpleDateFormat(pattern); return (fd.format(myDate)); } public static String formatDateTime(String myDate, String pattern) { if (myDate.length() > 10) myDate = myDate.substring(0, 10); Date date = getDateByString(myDate, "yyyy-MM-dd"); return formatDateTime(date, pattern, Locale.US); } public static String formatDateTime(Date myDate, String pattern, Locale localcode) { myDate = isDate(myDate); SimpleDateFormat fd = new SimpleDateFormat(pattern, localcode); return (fd.format(myDate)); } public static String formatDateTime(Date myDate) { return formatDateTime(myDate, patternDateTime); } public static Date isDate(Date myDate) { // if (myDate == null) // return new Date(); return myDate; } public static Date getDateByString(String rq) { // DateFormat df = DateFormat.getDateInstance(); DateFormat df = new SimpleDateFormat(patternDate); Date d = null; try { d = df.parse(rq); } catch (Exception e) { // e.printStackTrace(); } return d; } public static Date getDateByString(String str, String pattern) { SimpleDateFormat sdf = null; try { sdf = new SimpleDateFormat(pattern); } catch (Exception ex) { sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } try { return sdf.parse(str); } catch (Exception e) { } return null; } public static Date parse(String param) { Date date = null; SimpleDateFormat sdf = new SimpleDateFormat(patternDate); try { date = sdf.parse(param); } catch (Exception ex) { ex.printStackTrace(); } return date; } }