We would like to know how to check the date format of current string is according to required format or not.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; //from w w w.j a va 2 s. co m public class Main { public static void main(String[] args) { System.out.println("isValid - dd/MM/yyyy with 20150925 = " + isValidFormat("dd/MM/yyyy", "20150925")); System.out.println("isValid - dd/MM/yyyy with 25/09/2015 = " + isValidFormat("dd/MM/yyyy", "25/09/2015")); } public static boolean isValidFormat(String format, String value) { Date date = null; try { date = new SimpleDateFormat(format).parse(value); } catch (ParseException ex) { ex.printStackTrace(); } return date != null; } }
The code above generates the following result.