Here you can find the source of format(Object value, String pattern)
public static String format(Object value, String pattern)
//package com.java2s; /******************************************************************************* * Copyright 2007-2013 See AUTHORS file./*w ww. ja va2 s . co m*/ * * 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.DecimalFormat; import java.text.FieldPosition; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static final String VALUE_BLANK = ""; public static String format(Object value, String pattern) { if (value == null) return VALUE_BLANK; if (value instanceof Number) { DecimalFormat df = null; if (pattern == null || VALUE_BLANK.equals(pattern)) { df = new DecimalFormat("#.####################"); } else { df = new DecimalFormat(pattern); } return df.format(value, new StringBuffer(), new FieldPosition(0)).toString(); } else if (value instanceof Date) { SimpleDateFormat sf = null; if (pattern == null || VALUE_BLANK.equals(pattern)) { sf = new SimpleDateFormat("yyyy-MM-dd"); } else { sf = new SimpleDateFormat(pattern); } return sf.format(value); } else { return value.toString(); } } }