patternjavaMinor
Check whether a date is a valid future date
Viewed 0 times
datefuturevalidcheckwhether
Problem
I'm looking to increase the conciseness of this code. I realize that I can use Joda (or Java 8's new date API), but if I were to keep this to just Java 7, any suggestions? (I care less about whitespace and formatting.)
```
/**
* Tests whether the date input represents
* a real date in mm/dd/YYYY format that is after the current date.
* Useful for testing send dates and expiration dates.
*
* Tests against current date minus 24 hours
* so users in different time zones from the server can
* be accommodated.
*
* @param pDateString date to be tested
* @return true if date (@12:01 am) >= yesterday (@12:02am)
*/
public static boolean isValidFutureDate(String pDateString) {
if (!isValidDate(pDateString)) {
return false;
}
StringTokenizer st = new StringTokenizer(pDateString.trim(), "/");
if (st.countTokens() != 3) {
throw new NumberFormatException("Date format should be MM/DD/YYYY.");
}
String month = st.nextToken();
String day = st.nextToken();
String year = st.nextToken();
long oneDayInMillis = 86400000;
// set reference point to yesterday's date, 12:01am
GregorianCalendar ref = new GregorianCalendar();
ref.setTime(new Date(System.currentTimeMillis() - oneDayInMillis));
ref.set(Calendar.HOUR_OF_DAY, 0);
ref.set(Calendar.MINUTE, 1);
ref.set(Calendar.AM_PM, Calendar.AM);
// set comparison time to entered day, 12:02am
GregorianCalendar now = toDate(year, month, day);
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 2);
ref.set(Calendar.AM_PM, Calendar.AM);
return now.after(ref);
}
/**
* This method tests whether the date string passed in represents
* a real date in mm/dd/YYYY format.
*
* @param pDateString date to be tested
* @return a boolean value
*/
public static boolean isValidDate(String pDateString) {
StringTokenizer st= new StringTokenizer(pDateString.trim(), "/");
if (st.countTokens() != 3) {
throw new NumberFormatExc
```
/**
* Tests whether the date input represents
* a real date in mm/dd/YYYY format that is after the current date.
* Useful for testing send dates and expiration dates.
*
* Tests against current date minus 24 hours
* so users in different time zones from the server can
* be accommodated.
*
* @param pDateString date to be tested
* @return true if date (@12:01 am) >= yesterday (@12:02am)
*/
public static boolean isValidFutureDate(String pDateString) {
if (!isValidDate(pDateString)) {
return false;
}
StringTokenizer st = new StringTokenizer(pDateString.trim(), "/");
if (st.countTokens() != 3) {
throw new NumberFormatException("Date format should be MM/DD/YYYY.");
}
String month = st.nextToken();
String day = st.nextToken();
String year = st.nextToken();
long oneDayInMillis = 86400000;
// set reference point to yesterday's date, 12:01am
GregorianCalendar ref = new GregorianCalendar();
ref.setTime(new Date(System.currentTimeMillis() - oneDayInMillis));
ref.set(Calendar.HOUR_OF_DAY, 0);
ref.set(Calendar.MINUTE, 1);
ref.set(Calendar.AM_PM, Calendar.AM);
// set comparison time to entered day, 12:02am
GregorianCalendar now = toDate(year, month, day);
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 2);
ref.set(Calendar.AM_PM, Calendar.AM);
return now.after(ref);
}
/**
* This method tests whether the date string passed in represents
* a real date in mm/dd/YYYY format.
*
* @param pDateString date to be tested
* @return a boolean value
*/
public static boolean isValidDate(String pDateString) {
StringTokenizer st= new StringTokenizer(pDateString.trim(), "/");
if (st.countTokens() != 3) {
throw new NumberFormatExc
Solution
Correctness
My first point is that the current date is accepted as a valid future date. That is odd, and it is certainly not according to :
Tests whether the date input represents a real date in mm/dd/YYYY
format that is after the current date.
Simplicity
Secondly, assuming the current date should return false, the entire implementation can be as simple as :
Testable
Lastly, this method is difficult to test. Does it work when now falls in a DST overlap period? Does it work on 29th of February? Does it work in the time zone "Australia/Darwin"?
While you may not use Java 8, you can certainly make a
My first point is that the current date is accepted as a valid future date. That is odd, and it is certainly not according to :
Tests whether the date input represents a real date in mm/dd/YYYY
format that is after the current date.
Simplicity
Secondly, assuming the current date should return false, the entire implementation can be as simple as :
public static boolean isValidDate(String pDateString) throws ParseException {
Date date = new SimpleDateFormat("MM/dd/yyyy").parse(pDateString);
return new Date().before(date);
}Testable
Lastly, this method is difficult to test. Does it work when now falls in a DST overlap period? Does it work on 29th of February? Does it work in the time zone "Australia/Darwin"?
While you may not use Java 8, you can certainly make a
Clock abstraction of your own to get around this.Code Snippets
public static boolean isValidDate(String pDateString) throws ParseException {
Date date = new SimpleDateFormat("MM/dd/yyyy").parse(pDateString);
return new Date().before(date);
}Context
StackExchange Code Review Q#79913, answer score: 4
Revisions (0)
No revisions yet.