Here you can find the source of dateTimeFormat(String dateTime)
public static String dateTimeFormat(String dateTime)
//package com.java2s; /**************************************************************************** * Copyright (C) 2014 www.apkdv.com * Author: LengYue ProjectName: DvTools * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License./*from w ww . j a v a2 s . com*/ *****************************************************************************/ public class Main { public static String dateTimeFormat(String dateTime) { StringBuilder sb = new StringBuilder(); try { 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; } }