-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeliverable2.java
More file actions
56 lines (46 loc) · 1.86 KB
/
deliverable2.java
File metadata and controls
56 lines (46 loc) · 1.86 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.time.LocalDate;
import java.time.Period;
import java.util.Scanner;
public class deliverable2 {
public static void main(String[] args) {
LocalDate firstDate = LocalDate.of(1999, 04, 30);
LocalDate secondDate = LocalDate.now();
Period differenceDates = Period.ZERO;
int firstEntry = 0;
Scanner scnr = new Scanner(System.in);
boolean playing = true;
String answer = "";
// run over and over to test different scenarios
while (playing) {
System.out.println("This is a program that calculates the amount of time between two dates. ");
// these try /catch blocks detect invalid input
try {
System.out.println("Please enter your first date in the format: yyyy mm dd");
firstDate = LocalDate.of(scnr.nextInt(), scnr.nextInt(), scnr.nextInt());
} catch (Exception e) {
System.out.println("Please make sure you entered the appropriate value.");
}
try {
System.out.println("Please enter your second date in the format: yyyy mm dd");
secondDate = LocalDate.of(scnr.nextInt(), scnr.nextInt(), scnr.nextInt());
} catch (Exception e) {
System.out.println("Please make sure you entered the appropriate value.");
}
differenceDates = Period.between(firstDate, secondDate);
System.out.println("The amount of time that has passed between the two dates is: "
+ differenceDates.getYears() + " years, " + differenceDates.getMonths() + " months, and "
+ differenceDates.getDays() + " days.");
System.out.println("Dates are " + firstDate + " " + secondDate);
System.out.println("Would you like to try new dates? Please Type y to continue");
try {
answer = scnr.next();
} catch (Exception e) {
System.out.println("Please make sure you entered the appropriate value.");
}
// technically anything starting with y will continue the loop
if (answer.charAt(0) != 'y') {
playing = false;
}
}
}
}