DESKTOP-O1UAITJ_20200904-史鹏华
一,问题
刚开始程序正常运行为什么多运行几次就会报错
package shipenghau.anonymous;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
///**在 CustomerTest 类的 main 方法完成对 Customer 数组的排序:
public class CustomerTest {
public static void main( String[] args ) {
final Random random = new Random();
final Customer[] customers = new Customer[ 5 ] ;
// 使用循环对 customers 数组进行初始化
for( int i = 0 ; i < customers.length ; i++){
// 随机产生 年份、月份、日期,并将据此创建 LocalDate 对象中
LocalDate date = LocalDate.of(random.nextInt(2020),random.nextInt(11),random.nextInt(31));
// 在这里创建 Customer 对象
customers[i]=new Customer(date );
}
System.out.println("排序前的数组为");
for (int i = 0; i < customers.length; i++) {
System.out.println(customers[i]);
}
// // 使用循环输出排序前的 customers
//
// // 使用 【匿名类】实现 比较器接口 ,以便于按照 Customer 的 birthdate 升序排序
Comparator<Customer> comparator = new Comparator<Customer>() {
@Override
public int compare(Customer o1, Customer o2) {
if (o1.getBirthdate()!=null&&o2.getBirthdate()!=null) {
if (o1.getBirthdate().isAfter(o2.getBirthdate())) {
return 1;
}else if(o1.getBirthdate().isBefore(o2.getBirthdate()))
return -1;
}
return 0;
}
};
Arrays.sort( customers , comparator ); // 用比较器排序
// // 使用循环输出排序后的日期
System.out.println("排序后的数组为");
for (int j = 0; j < customers.length; j++) {
System.out.println(customers[j]);
}
}
}
Exception in thread "main" java.time.DateTimeException: Invalid value for DayOfMonth (valid values 1 - 28/31): 0
at java.base/java.time.temporal.ValueRange.checkValidValue(ValueRange.java:311)
at java.base/java.time.temporal.ChronoField.checkValidValue(ChronoField.java:717)
at java.base/java.time.LocalDate.of(LocalDate.java:270)
at shipenghau.anonymous.CustomerTest.main(CustomerTest.java:22)
二,吐槽
最近知识量有点大,内存快满了。
近期评论