Java yyyy format(Object value, String pattern)

Here you can find the source of format(Object value, String pattern)

Description

format

License

Apache License

Declaration

public static String format(Object value, String pattern) 

Method Source Code

//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();
        }
    }
}

Related

  1. format(final Date date, final String pattern)
  2. format(final java.util.Date date, final String sFormat)
  3. format(java.util.Date date)
  4. format(java.util.Date date)
  5. format(java.util.Date date, String pattern)
  6. format(ResourceBundle idioma, Date date)
  7. format(String dateString)
  8. format(String oldPattern, String newPattern, String data)
  9. format2(Date data)