我正在尝试从字符串中设置并返回最早的日期,并且我认为在设置日期时我遗漏了一些东西,因为每当我尝试设置 Date 的值时,我都会不断收到 nullreferenceexception。谢谢你的帮助
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;
}
}