-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.java
More file actions
92 lines (79 loc) · 1.65 KB
/
Time.java
File metadata and controls
92 lines (79 loc) · 1.65 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
//cbasurto: Hw 1 problem 2 defining time data
import java.util.Calendar;
public class Time
{
//fields
private int hours;
private int minutes;
public static final Time NOON = new Time(12,0);
//constructors
public Time(){
hours = 4;
minutes = 9;
}
//standard constructor
public Time (int h, int m){
hours = (h+m/60)%24;
minutes = m%60;
}
public Time(String tstring) {
String[] strsplit = tstring.split(":");
hours = Integer.parseInt(strsplit[0]);
minutes = Integer.parseInt(strsplit[1]);
}
//getters
public int getHours(){
return hours;
}
public int getMinutes(){
return minutes;
}
public Time getNOON(){
return NOON;
}
//mutator to hrs later
public int addHours(int hrs){
hours = hours + hrs;
hours = hours%24;
return hours;
}
//toString method for 24 hour notation
public String toString (){
if (minutes <10)
if (hours <12)
return "" +hours+ ":0" + +minutes+ "AM";
else
return "" +hours+ ":" + +minutes+ "PM";
else
if (hours <12)
return "" +hours+ ":" + +minutes+ "AM";
else
return "" +hours+ ":" + +minutes+ "PM";
}
//method for am/pm string
public String amPM () {
if (hours <12)
return "AM";
else
return "PM";
}
//boolean method for lessthan noon
public boolean lessThan(Time t) {
if(hours < t.getHours())
return true;
else if(hours > t.getHours())
return false;
if(minutes < t.getMinutes())
return true;
else if(minutes > t.getMinutes())
return false;
return false;
}
//boolean method for equal noon
public boolean equals(Time t) {
if(hours == t.getHours() && minutes == t.getMinutes())
return true;
else
return false;
}
}