Here you can find the source of dateTimeFormat(String dateTime)
public static String dateTimeFormat(String dateTime)
//package com.java2s; public class Main { public static String dateTimeFormat(String dateTime) { StringBuilder sb = new StringBuilder(); try {//from w w w . j ava2s . c o m if (isEmpty(dateTime)) { return null; } String[] dateAndTime = dateTime.split(" "); if (dateAndTime.length > 0) { for (String str : dateAndTime) { if (str.indexOf("-") != -1) { String[] date = str.split("-"); for (int i = 0; i < date.length; i++) { String str1 = date[i]; sb.append(strFormat2(str1)); if (i < date.length - 1) { sb.append("-"); } } } else if (str.indexOf(":") != -1) { sb.append(" "); String[] date = str.split(":"); for (int i = 0; i < date.length; i++) { String str1 = date[i]; sb.append(strFormat2(str1)); if (i < date.length - 1) { sb.append(":"); } } } } } } catch (Exception e) { e.printStackTrace(); return null; } return sb.toString(); } public static boolean isEmpty(String str) { return str == null || str.trim().length() == 0; } public static String strFormat2(String str) { try { if (str.length() <= 1) { str = "0" + str; } } catch (Exception e) { e.printStackTrace(); } return str; } }