-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidDate.java
More file actions
217 lines (203 loc) · 8.11 KB
/
validDate.java
File metadata and controls
217 lines (203 loc) · 8.11 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
/**
* Class which creates a Date (java.util) object
* Note: the date object will have the format:
* day of week month date of day hours:min:seconds GMT yyyy
* @author Nick
*/
public class validDate{
private static Date date;
/**
* Let the user create a date object in the right format (don't check yet for future or past
* and return the valid date.
* @throws IOException
*/
public void newDate() throws IOException {
System.out.println("Enter a date in 'dd/MM/yyyy' format:");
assignDate();
}
/**
* Let the user input until a valid date is given.
* @throws IOException
*/
private void assignDate() throws IOException{
while(true){
String userInput = enterDate();
if(validFormat(userInput)){
System.out.println("Date added successfully");
return;
}
else{
System.out.println("Either the format is wrong, or the date does not exist, please try again.");
}
}
}
/**
* Method will return the string in the format "dd.mm.yyyy" making use
* of the format specified at the beginning of the class
* @return String in the right format for further processing
*/
public String getDate(){
String[] elements = date.toString().split(" ");
return elements[2] + "/" + processMonth(elements[1]) + "/" + elements[5];
}
/**
* Method which gets passed a month as a String and returns the number of the month
* @param month String for month EX: Jan for January
* @return String representing number ex "01" for January
*/
private static String processMonth(String month){
switch (month){
case "Jan": return "01";
case "Feb": return "02";
case "Mar": return "03";
case "Apr": return "04";
case "May": return "05";
case "Jun": return "06";
case "Jul": return "07";
case "Aug": return "08";
case "Sep": return "09";
case "Oct": return "10";
case "Nov": return "11";
case "Dec": return "12";
default:
return "";
}
}
/**
* Accept input from the user
* @return String inputted by the user
* @throws IOException
*/
private String enterDate() throws IOException{
return App.userIn.readLine();
}
/**
* Method checks if the input is in the right format (dd/MM/yyyy)
* @param input String the user inputted
* @return true if the format is correct
* false otherwise
*/
public static boolean validFormat(String input){
// set the format
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
format.setLenient(false);
try {
date = format.parse(input);
return true;
} catch (ParseException e) {
return false;
}
}
/**
* Method checks if the date object created in this class is after or before
* the current date of today.
* Maybe add another similar method to compare this date (the one created in the method)
* with a different one passed as an argument.
* We'll see later
* @return true if date is in the future
* false if it is in the past
*/
private boolean futureDate(){
Date currentDate = new Date();
return date.after(currentDate);
}
/**
* Method checks if date is after a different specified date.
* This method will be used if they want to set up a budget starting and finishing at two different dates in the future
*
*/
public static boolean futureDateCustom(Date customDate){
return date.after(customDate);
}
/**
* Method accepts input, creates a date object and makes it valid only if it is in the future
* Good for the future.
* @return Date object when valid
* @throws IOException
*/
public void checkDate() throws IOException{
System.out.println("Enter a date in 'dd/MM/yyyy' format:");
while(true){
String userInput = enterDate();
if (validFormat(userInput)){
if (futureDate()){
return ;
}else{
System.out.println("Date entered is in the past.");
}
}else{
System.out.println("Date does not follow 'dd/MM/yyyy' format.");
}
}
}
/**
* Takes two String dates in form dd/mm/yyyy and checks if the second if later than the first
* NOTE: Assumes both are valid dates
*
* @param date1 String in form dd/mm/yyyy
* @param date2 String in form dd/mm/yyyy
* @return true is second date comes after first date
* @throws NumberFormatException dd, mm or yyyy is not an int
* @throws ArrayIndexOutOfBoundsException not in format dd/mm/yyyy (i.e. too short)
*/
public static boolean compareStringDates(String date1, String date2) throws NumberFormatException,ArrayIndexOutOfBoundsException{
int date1Day = Integer.parseInt(date1.substring(0, 2));
int date2Day = Integer.parseInt(date2.substring(0, 2));
int date1Month = Integer.parseInt(date1.substring(3, 5));
int date2Month = Integer.parseInt(date2.substring(3, 5));
int date1Year = Integer.parseInt(date1.substring(6));
int date2Year = Integer.parseInt(date2.substring(6));
if (date2Year > date1Year) {
return true;
} else if (date2Year == date1Year && date2Month > date1Month) {
return true;
} else return date2Year == date1Year && date2Month == date1Month && date2Day > date1Day;
}
/**
* Works like validDate.compareStringDates(String, String) but will return true
* when date 2 is same day or later than day 1
*
* @see #compareStringDates(String, String)
* @param date1 String in form dd/mm/yyyy
* @param date2 String in form dd/mm/yyyy
* @param allowSameDay if true, will return true when date 2 is same day or later than day 1
* @return true is second date comes after first date
* @throws NumberFormatException dd, mm or yyyy is not an int
* @throws ArrayIndexOutOfBoundsException not in format dd/mm/yyyy (i.e. too short)
*/
public static boolean compareStringDates(String date1, String date2, boolean allowSameDay) throws NumberFormatException,ArrayIndexOutOfBoundsException{
int date1Day = Integer.parseInt(date1.substring(0, 2));
int date2Day = Integer.parseInt(date2.substring(0, 2));
int date1Month = Integer.parseInt(date1.substring(3, 5));
int date2Month = Integer.parseInt(date2.substring(3, 5));
int date1Year = Integer.parseInt(date1.substring(6));
int date2Year = Integer.parseInt(date2.substring(6));
if (date2Year > date1Year) {
return true;
} else if (date2Year == date1Year && date2Month > date1Month) {
return true;
} else {
if (allowSameDay) {
return date2Year == date1Year && date2Month == date1Month && date2Day >= date1Day;
} else{
return date2Year == date1Year && date2Month == date1Month && date2Day > date1Day;
}
}
}
public static String addMonths(String originalDate, int monthsToAdd){
String currentDay = originalDate.substring(0,2);
int currentMonth = Integer.parseInt(originalDate.substring(3,5));
int currentYear = Integer.parseInt(originalDate.substring(6));
int newMonth = currentMonth + monthsToAdd;
while (newMonth > 12) {
newMonth -= 12;
currentYear++;
}
return currentDay + "/" + newMonth + "/" + currentYear;
}
}