Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Time {
- private final int seconds;
- public Time(int h, int m, int s) {
- seconds = h * 3600 + m * 60 + s;
- }
- public Time(String time) {
- String[] parts = time.split(":");
- int total = 0;
- int f = 1;
- for(int i = parts.length-1; i >= 0; i--) {
- total += Integer.parseInt(parts[i])*f;
- f *= 60;
- }
- seconds = total;
- }
- public int getHours() {
- return seconds / 3600;
- }
- public int getMinutes() {
- return seconds % 3600 / 60;
- }
- public int getSeconds() {
- return seconds % 3600 % 60;
- }
- @Override
- public String toString() {
- return getHours() + ":" + getMinutes() + ":" + getSeconds();
- }
- public Time add(Time t) {
- return new Time(0, 0, seconds + t.seconds);
- }
- public static void main(String[] args) {
- Time t1 = new Time(1, 20, 5);
- Time t2 = new Time("45:0");
- System.out.println(t1);
- System.out.println(t2);
- Time t3 = t1.add(t2);
- System.out.println(t3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement