Get current date or time or date and time with your format.
/*
FORMAT DATE AND TIME: "yyyy/MM/dd HH:mm:ss"
FORMAT DATE: "yyyy-MM-dd"
*/
public static String getCurrentDateTimeWithFormat(String formatDateTime) {
DateFormat dateFormat = new SimpleDateFormat(formatDateTime);
Date date = new Date();
return dateFormat.format(date);
}
Check validate date with format.
public static boolean isValidDate(String strDate, String format) {
if (strDate != null && format != null) {
strDate = strDate.trim();
format = format.trim();
if (!strDate.isEmpty() && !format.isEmpty()) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
if (strDate.trim().length() != dateFormat.toPattern().length()) {
return false;
}
dateFormat.setLenient(false);
dateFormat.parse(strDate);
} catch (ParseException ex) {
return false;
}
}
}
return true;
}
0 comments:
Post a Comment