Java Date Create toDate(Object o)

Here you can find the source of toDate(Object o)

Description

to Date

License

Apache License

Declaration

public static Object toDate(Object o) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class Main {
    public final static String d_cst_fmt = "EEE MMM dd HH:mm:ss zzz yyyy";
    public final static String d_long_fmt = "yyyyMMddHHmmss";
    public final static String d_short_fmt = "yyyyMMdd";

    public static Object toDate(Object o) {
        SimpleDateFormat sdf = null;
        try {//from w ww.j av  a  2  s .  co m
            if (o instanceof Date) {
                return o;
            } else if (o instanceof String) {
                String text = ((String) o).trim();
                int len = text.length();
                if (len == d_cst_fmt.length()) {
                    return new SimpleDateFormat(d_cst_fmt, Locale.US).parse(text);
                } else if (len == d_long_fmt.length()) {
                    return new SimpleDateFormat(d_long_fmt).parse(text);
                } else if (len == d_short_fmt.length()) {
                    return new SimpleDateFormat(d_short_fmt).parse(text);
                }
            }
            return null;
        } catch (Exception e) {
            System.err.println("Can't convert date : " + o);
            return null;
        }
    }

    public static int length(Object fv) {
        return (fv != null) ? fv.toString().trim().length() : 0;
    }
}

Related

  1. toDate(long lastModified)
  2. toDate(long segundos)
  3. toDate(long time)
  4. toDate(long time)
  5. ToDate(Object o)
  6. toDate(Object object)
  7. toDate(Object value)
  8. toDateFormat(Date date)
  9. toDateFromLong(Long time)