-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateAndTime.java
More file actions
138 lines (103 loc) · 2.15 KB
/
DateAndTime.java
File metadata and controls
138 lines (103 loc) · 2.15 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
//This class takes the Date.java class and inherites it to create Date and Time.
public class DateAndTime extends Date{
int hour, minute, second;
//creates DateAndTime object with default values
public DateAndTime(){
super();
hour =0;
minute = 0;
second = 0;
}
//sets the date and time returns false otherwise
public boolean SetDateAndTime(int hrs, int mins, int secs, int mon, int dy, int yr){
if(super.setDate(mon, dy, yr)){
if((hrs<24 && hrs>-1)&&(mins<60&&mins>-1)&&(secs<60&&secs>-1)){
hour = hrs;
minute = mins;
second = secs;
return true;
}
else
return false;
}
else
return false;
}
//increases second by one, adjusts hour, minutes, and day accordingly
public void increaseSecond(){
second++;
if (second ==60) {
second = 0;
minute++;
if (minute==60) {
minute = 0;
hour++;
if (hour ==24) {
hour = 0;
super.increaseDay();
}
}
}
}
//decreases second by one, adjusts hour, minutes, and day accordingly
public void decreaseSecond(){
second--;
if (second<0) {
second =59;
minute--;
if (minute<0) {
minute = 59;
hour--;
if (hour<0) {
hour = 23;
super.decreaseDay();
}
}
}
}
//increase second 3600 times to increase hour
public void increaseHour(){
for (int x=0;x<3600 ; x++) {
increaseSecond();
}
}
//decrease second 3600 times to increase hour
public void decreaseHour(){
for (int x=0;x<3600 ; x++) {
decreaseSecond();
}
}
public void increaseDay(){
super.increaseDay();
}
//calls date class to decrease day
public void decreaseDay(){
super.decreaseDay();
}
//returns the string in the Monthname, day, year HH:MM:SS AM/PM format
public String toString(){
String str = super.toString();
int num;
if(hour>12)
num = hour -12;
else
num = hour;
if(hour<10)
str = str + " 0"+num+":";
else
str = str+" "+num+":";
if(minute<10)
str = str + "0"+minute+":";
else
str = str+minute+":";
if(second<10)
str = str + "0"+second;
else
str = str+second;
if (hour<12)
str = str + " AM";
else
str = str + " PM";
return str;
}
}