-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifferentApproach.txt
More file actions
102 lines (84 loc) · 2.08 KB
/
differentApproach.txt
File metadata and controls
102 lines (84 loc) · 2.08 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
package app;
public class Main {
private static final int MINUTE = 60;
private static final int HOUR = 3600;
private static final int DAY = 86400;
private static final int WEEK = 604800;
private static final int MONTH = 2628288;
private static final Integer SECOND = 59;
private static int totalMonths = 0;
private static int totalWeeks = 0;
private static int totalDays = 0;
private static int totalHours = 0;
private static int totalMinutes = 0;
public static String formatDuration(int seconds) {
// handle edge case (now)
if (seconds == 0) {
return "now";
}
// handle edge case (1 second)
if (seconds == 1) {
return "1 second";
}
if (seconds > MONTH) {
totalMonths = seconds / MONTH;
seconds = seconds - (totalMonths * MONTH);
}
if (seconds > WEEK) {
totalWeeks = seconds / WEEK;
seconds = seconds - (totalWeeks * WEEK);
}
if (seconds > DAY) {
totalDays = seconds / DAY;
seconds = seconds - (totalDays * DAY);
}
if (seconds > HOUR) {
totalHours = seconds / HOUR;
seconds = seconds - (totalHours * HOUR);
}
if (seconds > MINUTE) {
totalMinutes = seconds / MINUTE;
seconds = seconds - (totalMinutes * MINUTE);
}
return concatenateReturnValue();
}
public static String concatenateReturnValue() {
String returnValue = "";
if (totalMonths != 0) {
if (totalMonths == 1) {
returnValue = totalMonths + "months";
} else {
returnValue = totalMonths + "1 month";
}
}
if (totalWeeks != 0) {
if (totalWeeks == 1) {
returnValue = totalWeeks + "weeks";
} else {
returnValue = totalWeeks + "1 week";
}
}
if (totalDays != 0) {
if (totalDays == 1) {
returnValue = totalDays + "days";
} else {
returnValue = totalDays + "1 day";
}
}
if (totalHours != 0) {
if (totalHours == 1) {
returnValue = totalHours + "hours";
} else {
returnValue = totalHours + "1 hour";
}
}
if (totalMinutes != 0) {
if (totalMinutes == 1) {
returnValue = totalMinutes + "minutes";
} else {
returnValue = totalMinutes + "1 minute";
}
}
return returnValue;
}
}