DateUtil { public static Date stringToDate(String dateString, String dateFromat){ SimpleDateFormat simpleDateFormat = null; Date date = null; if(!StringUtils.isEmpty(dateString) && !StringUtils.isEmpty(dateFromat)){ try{ simpleDateFormat = new SimpleDateFormat(dateFromat); date = simpleDateFormat.parse(dateString); }catch(Exception ex){ return null; } } return date; }  Date getDateFromNow(int days); Date getDateFromNow(int days, int hours, int minutes); static String dateToString(Date date, String dateFromat); Date getDateFromGivenDate(Date date, int days, int hours,int minutes, int second); Date addTimeOffset(Date date, int mins); static Date getUTCDate(); static Date getUTCDate(long epoh); static Date getLocalDateForUTCDate(Date date); static Date stringToDate(String dateString, String dateFromat);  }
@Test public void testStringToDate(){ String dateString = "2018-05-31"; String dateFormat = "yyyy-MM-dd"; Calendar cal = Calendar.getInstance(); cal.set(2018 - 1900, 04, 31); Date expectedDate = new Date(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DATE)); Date actualDate = dateUtil.stringToDate(dateString, dateFormat); if(actualDate != null){ Assert.assertEquals(expectedDate.getYear(), actualDate.getYear()); Assert.assertEquals(expectedDate.getDay(), actualDate.getDay()); Assert.assertEquals(expectedDate.getMonth(), actualDate.getMonth()); } }