Here you can find the source of convertToDate(String dateAsString)
Parameter | Description |
---|
Parameter | Description |
---|---|
ParseException | if the date can not be converted |
public static Date convertToDate(String dateAsString) throws ParseException
//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; } }