-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.java
More file actions
100 lines (76 loc) · 1.59 KB
/
Date.java
File metadata and controls
100 lines (76 loc) · 1.59 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
//cbasurto: Hw 1 problem 1 defining Date class with toString method.
import java.util.Calendar;
public class Date
{
//fields
private int day;
private int month;
private int year;
//constructors
public Date(){
day = 23;
month = 04;
year = 1991;
}
//standard constructor
public Date (int m, int d, int y){
day = d;
month = m;
year = y;
}
public Date(String sdate) {
String[] strsplit = sdate.split("/");
day = Integer.parseInt(strsplit[1]);
month = Integer.parseInt(strsplit[0]);
year = Integer.parseInt(strsplit[2]);
}
//getters
public int getDay()
{
return day;
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
//to return today
public static Date today(){
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DAY_OF_MONTH);
int year = c.get(Calendar.YEAR);
Date today = new Date(month,day,year);
return today;
}
//boolean non-static lessThan method
public boolean lessThan(Date d){
if(year < d.getYear())
return true;
else if(year > d.getYear())
return false;
if(month < d.getMonth())
return true;
else if(month > d.getMonth())
return false;
if(day < d.getDay())
return true;
else if(day > d.getDay())
return false;
return false;
}
//boolean equal method
public boolean equals(Date d){
if(day==d.getDay() && month==d.getMonth() && year==d.getYear())
return true;
else
return false;
}
//toString method to return date
public String toString(){
return ""+month+" / "+day+" / "+year+"";
}
}