---
# System prepended metadata

title: Java 日期的排序、最早、最晚
tags: [date, sort, java]

---

# Java 日期的排序、最早、最晚

###### tags: `java` `date` `sort`

## 初始化

```
final long now = new Date().getTime();
List<Date> listOfDates = new ArrayList<>();
```

## 產生16筆日期

```
Random random = new Random(
	System.currentTimeMillis()
);
for (int i = 0; i < 16; i++) {
	listOfDates.add(
		new Date(
			now + random.nextInt()
		)
	);
}
```

## 排序

```
Collections.sort(listOfDates);
```

## 轉換為陣列

```
return listOfDates.toArray(new Date[0]);
```

## 最大值

```
return Collections.max(listOfDates);
```


## 最小值

```
return Collections.min(listOfDates);
```
