-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatesArraySorter.java
41 lines (34 loc) · 1.1 KB
/
DatesArraySorter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.javamultiplex.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;
public class DatesArraySorter {
public static void main(String[] args) throws ParseException {
Scanner input = new Scanner(System.in);
System.out.println("Enter how many dates you want to sort?");
int size = input.nextInt();
String[] dates = new String[size];
System.out.println("Enter dates in MM/dd/yyyy format");
for (int i = 0; i < size; i++) {
dates[i] = input.next();
}
input.close();
String[] output = getSortedDateArray(dates);
System.out.println(Arrays.toString(output));
}
private static String[] getSortedDateArray(String[] dates) throws ParseException {
int length = dates.length;
SimpleDateFormat myDateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date[] myDates = new Date[length];
for (int i = 0; i < length; i++) {
myDates[i] = myDateFormat.parse(dates[i]);
}
Arrays.sort(myDates);
for (int i = 0; i < length; i++) {
dates[i] = myDateFormat.format(myDates[i]);
}
return dates;
}
}