forked from vdt/Algorithmic-Trading-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimestamp.java
More file actions
executable file
·349 lines (305 loc) · 8.66 KB
/
Timestamp.java
File metadata and controls
executable file
·349 lines (305 loc) · 8.66 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package TestJavaClient;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Timestamp implements Comparable, Serializable {
private static final long serialVersionUID = 4770641853221522355L;
String date;
String time;
/**
* Timestamp is a class that will contain the date and time of a transaction.
*
* the format is yyyymmdd hh:mm:ss
*/
public Timestamp() {
DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
Date d = new Date();
String temp[] = df.format(d).split(" ");
date = temp[0];
time = temp[1];
}
/**
* Compares the date of one Timestamp object (this)
* to the date of another Timestamp object (other)
* and returns -1 if this is less than other, 1 if
* this is greater than other, and 0 if they are equal
*
* @param t
* @return
*/
public int compareDates(Timestamp t) {
int year = Integer.parseInt(date.substring(0,3));
int month = Integer.parseInt(date.substring(4,5));
int day = Integer.parseInt(date.substring(6,7));
int oYear = Integer.parseInt(date.substring(0,3));
int oMonth = Integer.parseInt(date.substring(4,5));
int oDay = Integer.parseInt(date.substring(6,7));
if(year < oYear) {
return -1;
}
if(year > oYear) {
return 1;
}
if(month < oMonth) {
return -1;
}
if(month > oMonth) {
return 1;
}
if(day < oDay) {
return -1;
}
if(day > oDay) {
return 1;
}
return 0;
}
/**
* compares the time of one Timestamp object (this) and
* another Timestamp object (other). If this time is greater,
* returns a positive integer (1). If this time is less,
* returns a negative integer (-1). If the time is the same,
* returns 0.
*
* @param t, the other Timestamp
* @return
*/
public int compareTimes(Timestamp t) {
System.out.println(time);
System.out.println(time);
String timeParts[] = time.split(":");
int hours = Integer.parseInt(timeParts[0]);
int minutes = Integer.parseInt(timeParts[1]);
int seconds = Integer.parseInt(timeParts[2]);
String otherTP[] = t.getTime().split(":");
int oHours = Integer.parseInt(otherTP[0]);
int oMinutes = Integer.parseInt(otherTP[1]);
int oSeconds = Integer.parseInt(otherTP[2]);
if(hours < oHours) {
return -1;
}
if(hours > oHours) {
return 1;
}
if(minutes < oMinutes) {
return -1;
}
if(minutes > oMinutes) {
return 1;
}
if(seconds < oSeconds) {
return -1;
}
if(seconds > oSeconds) {
return 1;
}
return 0;
}
public String getDate() {
return date;
}
public String getTime() {
return time;
}
/**
* returns how many hours apart this is from the other timestamp.
*
* @param t
* @return
*/
public int hoursApart(Timestamp t) {
int hours = 0;
int oHours = 0;
try {
hours = Integer.parseInt(time.split(":")[0]);
oHours = Integer.parseInt(t.getTime().split(":")[0]);
} catch(Exception e) {
System.out.println("Times are causing exceptions in hoursApart in the Timestamp class.");
}
return(hours - oHours);
}
/**
* returns how many hours apart
* @param t
* @return
*/
public int minutesApart(Timestamp t) {
int minutes = 0;
int oMinutes = 0;
try {
minutes = Integer.parseInt(time.split(":")[1]);
oMinutes = Integer.parseInt(t.getTime().split(":")[1]);
} catch(Exception e) {
System.out.println("Times are causing exceptions in minutesApart in the Timestamp class.");
}
return (minutes - oMinutes + (hoursApart(t) * 60));
}
public int secondsApart(Timestamp t) {
int seconds = 0;
int oSeconds = 0;
try {
seconds = Integer.parseInt(time.split(":")[2]);
oSeconds = Integer.parseInt(t.getTime().split(":")[2]);
} catch(Exception e) {
System.out.println("Times are causing exceptions in secondsApart in the Timestamp class.");
}
return (seconds - oSeconds + (minutesApart(t) * 60));
}
/**
* sets the date to the given date after checking the format and
* believability of the numbers. (e.g. date must be within acceptable
* range for that month)
*
* @param newDate, String, formatted yyyymmdd
* @throws TimeFormatException
*/
public void setDate(String newDate) {
String dateParts[] = new String[3];
dateParts[0] = newDate.substring(0,4);
dateParts[1] = newDate.substring(4,6);
dateParts[2] = newDate.substring(6,8);
try {
if(dateParts.length != 3) {
throw new TimeFormatException("TimeFormatException incorrect date format");
}
int year = Integer.parseInt(dateParts[0]);
int month = Integer.parseInt(dateParts[1]);
int day = Integer.parseInt(dateParts[2]);
if(month < 1 || month > 12) {
throw new TimeFormatException("TimeFormatException month out of bounds");
}
switch(month)
{
case 1: //January
if(day < 1 || day > 31) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 2: //February
if(day < 1 || ((day > 29 && year%4 == 0) || (day > 28 && year%4 != 0))) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 3: //March
if(day < 1 || day > 31) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 4: //April
if(day < 1 || day > 30) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 5: //May
if(day < 1 || day > 31) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 6: //June
if(day < 1 || day > 30) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 7: //July
if(day < 1 || day > 31) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 8: //August
if(day < 1 || day > 31) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 9: //September
if(day < 1 || day > 30) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 10: //October
if(day < 1 || day > 31) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 11: //November
if(day < 1 || day > 30) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
case 12: //December
if(day < 1 || day > 31) {
throw new TimeFormatException("TimeFormatException day out of bounds");
}
break;
default:
throw new TimeFormatException("TimeFormatException month out of bounds");
}
date = newDate;
}
catch(Exception e) {
}
}
/**
* setTime sets the time to the given time after
* checking that the format was correct
*/
public void setTime(String newTime) {
String timeParts[] = newTime.split(":");
try {
if(timeParts.length != 3) {
throw new TimeFormatException("TimeFormatException incorrect time format");
}
int hours = Integer.parseInt(timeParts[0]);
int minutes = Integer.parseInt(timeParts[1]);
int seconds = Integer.parseInt(timeParts[2]);
if(hours > 23 || hours < 0) {
throw new TimeFormatException("TimeFormatException hours out of bounds");
}
if(minutes > 59 || minutes < 0) {
throw new TimeFormatException("TimeFormatException minutes out of bounds");
}
if(seconds > 59 || seconds < 0) {
throw new TimeFormatException("TimeFormatException seconds out of bounds");
}
time = newTime;
System.out.println(time);
}
catch(Exception e) {
}
}
public String toString() {
return date + " " + time;
}
/**
* compareTo takes a Timestamp object and compares its date and
* time to another Timestamp object's date and time.
*
* @param arg0, the other Timestamp object. Must be a Timestamp.
*/
@Override
public int compareTo(Object arg0) {
Timestamp otherStamp = (Timestamp)arg0;
if(compareDates(otherStamp) == -1) {
return -1;
}
if(compareDates(otherStamp) == 1) {
return 1;
}
if(compareTimes(otherStamp) == -1) {
return -1;
}
if(compareTimes(otherStamp) == 1) {
return 1;
}
return 0;
}
private class TimeFormatException extends Exception {
/**
*
*/
private static final long serialVersionUID = -6432349077759830757L;
public TimeFormatException(String problem) {
System.out.println(problem);
}
}
}