Java Date from String convertToDate(String dateAsString)

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

Description

Convert a String into a date

License

Open Source License

Parameter

Parameter Description

Exception

Parameter Description
ParseException if the date can not be converted

Return

The java.util.Date version of the string

Declaration

public static Date convertToDate(String dateAsString) throws ParseException 

Method Source Code


//package com.java2s;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**/*from  www  . ja  v  a2 s.c  o m*/
     * Convert a String into a date
     * @param   dateAsString. A String entered that is to be converted
     * to a string.
     * @return   The java.util.Date version of the string
     * @throws  ParseException if the date can not be converted     
     */
    public static Date convertToDate(String dateAsString) throws ParseException {
        Date utilDate = null;

        if (isDatePopulated(dateAsString)) {
            DateFormat df = DateFormat.getInstance();
            df.setLenient(false);
            SimpleDateFormat sf = (SimpleDateFormat) df;
            sf.applyPattern("dd/MM/yyyy");
            utilDate = sf.parse(dateAsString);
        }
        return utilDate;
    }

    /**
     * Determine whether an attribute is populated.
     * @param attribute
     * @return true if the attribute contains a value.
     */
    public static boolean isDatePopulated(String attribute) {
        boolean populated = true;
        if ((attribute == null) || (attribute.trim().length() == 0) || (attribute.equalsIgnoreCase("dd/mm/yy"))
                || (attribute.equalsIgnoreCase("dd/mm/yyyy"))) {
            populated = false;
        }
        return populated;
    }
}

Related

  1. convertToDate(final String str, final DateFormat dateFormat)
  2. convertToDate(String date)
  3. convertToDate(String dateString)
  4. convertToDate(String dateString, String format)
  5. convertToDate(String dateString, String pattern)
  6. convertToDate(String ddMMyy)