Here you can find the source of formatDate(Object obj, String format)
public static String formatDate(Object obj, String format)
//package com.java2s; /**//w ww . j a va 2s.co m * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) * * 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. */ import java.text.SimpleDateFormat; public class Main { private static final String DAY_STR = "yyyy-MM-dd"; private static final String TIME_STR = "yyyy-MM-dd HH:mm:ss"; private static final String TIME__NO_S_STR = "yyyy-MM-dd HH:mm"; private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat( DAY_STR); private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat( TIME_STR); private static final SimpleDateFormat TIME__NO_S_FORMAT = new SimpleDateFormat( TIME__NO_S_STR); public static String formatDate(Object obj, String format) { if (obj == null || obj.toString() == "") { return ""; } SimpleDateFormat dateFormat = null; if (DAY_STR.equals(format)) { dateFormat = DAY_FORMAT; } else if (TIME_STR.equals(format)) { dateFormat = TIME_FORMAT; } else if (TIME__NO_S_STR.equals(format)) { dateFormat = TIME__NO_S_FORMAT; } else { dateFormat = new SimpleDateFormat(format); } return dateFormat.format(obj); } }