Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java code to determines if the given date is valid using Java Date APIs
02-06-2011, 08:51 AM
Post: #1
Java code to determines if the given date is valid using Java Date APIs
Java code to determines if the given date arugument is valid using Java Date APIs.Also, ensure dates are entered in "MM/dd/yyyy" format.The method isDateValid will return true if the passed date is Valid else return false if it is invalid.

Find below the code with example.

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

public class ValidDate {

public static void main(String[] args) {

//Valid Date
System.out.println(ValidDate.isDateValid("03/21/2007"));

//Invalid Date
System.out.println(ValidDate.isDateValid("03/32/2007"));
}

/**
* Determines if the date arguments is valid.
* assumes dates are entered in "MM/dd/yyyy" format
* @param String
* @return true if the date is valid; else false
*/
public static boolean isDateValid(String date) {
// takes space, :, /, -, and comma as valid token separators
StringTokenizer st = new StringTokenizer(date, " /:-,");
String[] datePart = new String[st.countTokens()];
int i = 0;
int month, day, year;
while (st.hasMoreTokens()) {
datePart[i] = st.nextToken();
i++;
}
try {
if (datePart[0] != null)
month = Integer.parseInt(datePart[0]);
else
return false;
if (datePart[1] != null)
day = Integer.parseInt(datePart[1]);
else
return false;
if (datePart[2] != null) {
if (datePart[2].length() < 4)
return false;
// immediately invalid if year is less than 4, not in yyyy format
year = Integer.parseInt(datePart[2]);
} else {
return false;
}
return isDateValid(year, month, day);
} catch (Exception e) {
e.printStackTrace();
return false;
}
} // end isDateValid(String date)

/**
* Determines if the date argument is valid
* @param year
* @param month
* @param day
* @return true if the date is valid; else false
*/
public static boolean isDateValid(int year, int month, int day) {
return isDateValid(year, month, day, 0, 0, 0);
}
public static boolean isDateValid(int year, int month, int day, int hour,
int minute, int second) {
int testYear;
int testMonth;
int testDay;
int testHour;
int testMinute;
int testSecond;
// adjust month because January is equal to 0 in Gregorian Calendar
month--;
// instantiate a Gregorian Calendar object with Date to validate
GregorianCalendar greg = new GregorianCalendar(year, month, day, hour,
minute, second);
testYear = greg.get(Calendar.YEAR);
testMonth = greg.get(Calendar.MONTH);
testDay = greg.get(Calendar.DATE);
testHour = greg.get(Calendar.HOUR_OF_DAY); // use 24 hour clock
testMinute = greg.get(Calendar.MINUTE);
testSecond = greg.get(Calendar.SECOND);
if (month != testMonth || day != testDay || year != testYear
|| hour != testHour || minute != testMinute
|| second != testSecond)
return false;
return true;
}

}
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:



Send your comments, Suggestions or Queries regarding this site at info@tutorials4u.net

Copyright © 2010 Tutorials4u.net All Rights Reserved