Java tutorial
//package com.java2s; //License from project: Open Source License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; public class Main { /** * Try to parse input with SimpleDateFormat * * @param input input. * @param format SimpleDateFormat * @param setYear1700 When true the age will be not displayed in brackets * @return Date object if successful, otherwise null */ private static Date parseStringWithSimpleDateFormat(String input, String format, boolean setYear1700) { return parseStringWithSimpleDateFormat(input, format, setYear1700, Locale.ENGLISH); } /** * Try to parse input with SimpleDateFormat * * @param input input. * @param format SimpleDateFormat * @param setYear1700 When true the age will be not displayed in brackets * @param locale locale. * @return Date object if successful, otherwise null */ private static Date parseStringWithSimpleDateFormat(String input, String format, boolean setYear1700, Locale locale) { SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale); dateFormat.setTimeZone(TimeZone.getDefault()); try { Date parsedDate = dateFormat.parse(input); /* * Because no year is defined in address book, set year to 1700 * * When year < 1800, the age will be not displayed in brackets */ if (setYear1700) { Calendar cal = Calendar.getInstance(); cal.setTime(parsedDate); cal.set(Calendar.YEAR, 1700); } return parsedDate; } catch (ParseException ignored) { return null; } } }