-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDate.java
78 lines (66 loc) · 1.97 KB
/
Date.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
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
package application;
import java.util.Objects;
/**
* Represents a simple date with a month, day, and year.
*/
public class Date {
private int day;
private int month;
private int year;
public Date(int month, int day, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
/**
* Returns this date in the YYYYMD format -- e.g. "2019-5-4" for May 4, 2019.
* @return this date in the YYYYMD format
*/
public String toYYYYMD() {
return year + "-" + month + "-" + day;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Date date = (Date) o;
return day == date.day &&
month == date.month &&
year == date.year;
}
@Override
public int hashCode() {
return Objects.hash(day, month, year);
}
/**
* Returns the date from the format "YYYY-M-D", e.g. "2019-5-4" for May 4, 2019. This is the format used in the CSV
* files.
* @param dateStr the date string
* @return a date object from the string
*/
public static Date fromYYYYMD(String dateStr) {
String[] split = dateStr.split("-");
if (split.length != 3)
throw new RuntimeException("Date is in invalid format");
int year;
int month;
int day;
try {
year = Integer.parseInt(split[0]);
month = Integer.parseInt(split[1]);
day = Integer.parseInt(split[2]);
} catch (NumberFormatException ex) {
throw new RuntimeException("Date is in invalid format", ex);
}
return new Date(month, day, year);
}
}