Java Parse Date parseDate(String value, Class targetType, String... formats)

Here you can find the source of parseDate(String value, Class targetType, String... formats)

Description

parse Date

License

Apache License

Declaration

public static Date parseDate(String value, Class targetType,
            String... formats) 

Method Source Code

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class Main {
    public static Date parseDate(String value, Class targetType,
            String... formats) {/*  w w  w.  j a  v  a2 s.  co  m*/
        for (String format : formats) {
            try {
                long v = new SimpleDateFormat(format).parse(value)
                        .getTime();
                return (Date) targetType.getConstructor(long.class)
                        .newInstance(v);
            } catch (ParseException e) {
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            try {
                return (Date) targetType.getConstructor(String.class)
                        .newInstance(value);
            } catch (Exception e) {
            }
        }
        throw new IllegalArgumentException("cannot parse:" + value
                + " for date by formats:" + Arrays.asList(formats));
    }
}

Related

  1. parseDate(String value)
  2. parseDate(String value)
  3. parseDate(String value)
  4. parseDate(String value)
  5. parseDate(String value)
  6. parseDate(String value, String formater)
  7. parseDate(String value, String pattern)
  8. parseDate2String(Date date)
  9. parseDate4UrlCreateTime(String dateStr)