Java Date from String convertToDate(String value)

Here you can find the source of convertToDate(String value)

Description

convert To Date

License

Open Source License

Declaration

public static Date convertToDate(String value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    static SimpleDateFormat FMT = new SimpleDateFormat("dd-MMM-yyyy");
    static SimpleDateFormat FMT2 = new SimpleDateFormat("yyyy-MM-dd");

    public static Date convertToDate(String value) {
        if (value == null)
            return null;
        value = value.trim();//w w  w . ja  va 2 s .  c om
        if (value.length() == 0)
            return null;

        Date date = null;
        try {
            date = FMT.parse(value);
        } catch (ParseException pe1) {
            try {
                date = FMT2.parse(value);
            } catch (ParseException pe2) {
                throw new RuntimeException("unable to parse date [" + value + "]");
            }
        }

        return date;
    }
}

Related

  1. convertToDate(String str)
  2. convertToDate(String str, String format)
  3. convertToDate(String strDate, String format)
  4. convertToDate(String strDate, String format)
  5. convertToDate(String val)
  6. convertToDateTimeNoSec(String date)
  7. convertToDateTimeString(Date d1)
  8. convertToDateWithTimeString(Date date)
  9. getDateFromPattern(String pattern)