Java Parse Date Pattern YYYY parse(Object object)

Here you can find the source of parse(Object object)

Description

parse

License

Apache License

Declaration

public static String parse(Object object) 

Method Source Code

//package com.java2s;
/*//from   w  ww. ja v a2 s  .  c  o  m
 * Copyright 2009-2012 the original author or authors.
 *
 * 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.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    private static Double DEFAULT_DOUBLE = new Double(0.0);
    private static Integer DEFAULT_INTEGER = new Integer(0);
    private static Long DEFAULT_LONG = new Long(0);

    public static String parse(Object object) {
        return parse(String.class, object);
    }

    @SuppressWarnings("unchecked")
    public static <T> T parse(Class<T> clazz, Object object) {
        T defaultValue = null;
        try {
            if (clazz == Date.class) {
                String stringValue = parse(String.class, object);
                return clazz.cast(new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy", Locale.US).parse(stringValue));
            }

            if (clazz == Integer.class) {
                defaultValue = (T) DEFAULT_INTEGER;
            } else if (clazz == Long.class) {
                defaultValue = (T) DEFAULT_LONG;
            } else if (clazz == Double.class) {
                defaultValue = (T) DEFAULT_DOUBLE;
            }

            if (object == null) {
                return defaultValue;
            }

            // special handling for int and long since smaller numbers become ints
            // but may be requested as long and vice versa
            if (clazz == Integer.class) {
                if (object instanceof Number) {
                    return clazz.cast(((Number) object).intValue());
                } else if (object instanceof String) {
                    return clazz.cast(Integer.valueOf(((String) object)));
                }
            }
            if (clazz == Long.class) {
                if (object instanceof Number) {
                    return clazz.cast(((Number) object).longValue());
                } else if (object instanceof String) {
                    return clazz.cast(Long.valueOf(((String) object)));
                }
            }
            if (clazz == Double.class) {
                if (object instanceof Number) {
                    return clazz.cast(((Number) object).doubleValue());
                } else if (object instanceof String) {
                    return clazz.cast(Double.valueOf(((String) object)));
                }
            }

            return clazz.cast(object);
        } catch (ClassCastException e) {
            // ignore
        } catch (ParseException e) {
            // ignore
        }
        return defaultValue;
    }
}

Related

  1. parse(final String s)
  2. parse(final String source)
  3. parse(final String source)
  4. parse(Long date)
  5. parse(Long time)
  6. parse(String calendarString)
  7. parse(String d)
  8. parse(String date)
  9. parse(String date)