List of utility methods to do Date from String
Date | convertToDate(final String str, final DateFormat dateFormat) convert To Date try { return dateFormat.parse(str); } catch (ParseException e) { return null; |
Date | convertToDate(String date) convert To Date SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); if (date != null && !date.isEmpty()) { return sdf.parse(date); return null; |
Date | convertToDate(String dateAsString) Convert a String into a date 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; |
Date | convertToDate(String dateString) convert To Date SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT); Date date = sdf.parse(dateString); return date; |
Date | convertToDate(String dateString, String format) convert To Date if (dateString == null || dateString.length() == 0) return null; SimpleDateFormat sdf; boolean defaultFlag = false; if (format != null && format.length() > 0) { sdf = new SimpleDateFormat(format); } else { sdf = new SimpleDateFormat("MM/dd/yyyy"); ... |
Date | convertToDate(String dateString, String pattern) concerts the string format into a Date if (dateString == null || dateString.trim().isEmpty()) { return null; if (pattern == null || pattern.isEmpty()) { throw new Exception("convertToDate failed: pattern cannot be null or empty"); try { SimpleDateFormat sdf = new SimpleDateFormat(pattern); ... |
Date | convertToDate(String ddMMyy) convert to date SimpleDateFormat format = new SimpleDateFormat("ddMMyy"); format.setLenient(false); Date date = null; date = format.parse(ddMMyy); return date; |
Date | convertToDate(String inDate) convert To Date Date date = null; for (SimpleDateFormat format : dateFormats) { format.setLenient(false); try { if (inDate.length() < 12 && format.toPattern().length() < 12) { date = format.parse(inDate.trim()); } else if (inDate.length() > 12 && format.toPattern().length() > 12) { date = format.parse(inDate.trim()); ... |
Date | convertToDate(String input) Convert String with various formats into java.util.Date Date date = null; if (null == input) { return null; for (SimpleDateFormat format : dateFormats) { try { format.setLenient(false); date = format.parse(input); ... |
String | convertToDate(String str) convert To Date if (str == null) return null; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); return sdf.format(sdf1.parse(str)); |