Java Date Class NullPointerException
I am trying to set and return the earliest date from a string and I think
I am missing something when setting my date as I keep getting a
nullreferenceexception whenever I try to set the values for Date. Thanks
for any help
private static Date createDate(String input)
{
Date date = null;
if (input == null)
return null;
// Split formatted input into separate values
String tempDates[] = input.split(dateSep);
// Store values as integers
int[] dateValues = {0, 0, 0};
dateValues[0] = Integer.parseInt(tempDates[0]);
dateValues[1] = Integer.parseInt(tempDates[1]);
dateValues[2] = Integer.parseInt(tempDates[2]);
// Sort integers from lowest to highest
Arrays.sort(dateValues);
// Set return date
date.setMonth(dateValues[0]);
date.setDate(dateValues[1]);
date.setYear(dateValues[2]);
System.out.println(date);
// Checking basic date restrictions
if (date.getMonth() <= 0 || date.getMonth() > 12)
throw new IllegalArgumentException("Month is not valid " + month);
if (date.getDay() <= 0 || date.getDay() > 31)
throw new IllegalArgumentException("Day is not valid " + day);
if (date.getYear() <= 0)
throw new IllegalArgumentException("Year is not valid " + year);
return date;
}
}
No comments:
Post a Comment