-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputExercise.java
More file actions
74 lines (66 loc) · 2.16 KB
/
InputExercise.java
File metadata and controls
74 lines (66 loc) · 2.16 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
package exercise_input;
import java.text.*;
import java.util.*;
import static java.lang.String.*;
import static java.lang.System.*;
import static java.util.TimeZone.*;
import static yan_service.YANConstant.*;
import static yan_service.YANService.*;
public class InputExercise {
public static void main(String[] args) {
// tit
out.println(BLUE_BOLD);
printlnAdv("Input Exersice");
// content
run();
}
// Fields
private static final double MIN_TIMEZONE = -11;
private static final double MAX_TIMEZONE = 12;
// Main
private static void run() {
// cap
out.println();
printAdv(GREEN, "Nhập vào múi giờ: ", RESET);
// format
var timeFormat = new SimpleDateFormat("HH:mm:ss");
timeFormat.setTimeZone(getTimeZone(numToUTC(timezoneLimit(MIN_TIMEZONE, MAX_TIMEZONE))));
// output
printlnAdv(YELLOW, format("Hiện giờ là: %s", timeFormat.format(new Date())));
out.println();
// ctrl
checkOut();
}
// Timezone limit
private static double timezoneLimit(double min, double max) {
var n = scanDub();
if (n < min || n > max) {
// min +-
var iMin = valueOf((int) min);
var sMin = valueOf(min);
// max +-
var iMax = valueOf((int) max);
var sMax = valueOf(max);
// main
printAdv(RED,
format("Múi giờ từ GMT%s đến GMT%s, xin nhập lại: ",
min == (int) min ? min > 0 ? "+" + iMin : iMin : min > 0 ? "+" + sMin : sMin,
max == (int) max ? max > 0 ? "+" + iMax : iMax : max > 0 ? "+" + sMax : sMax),
RESET);
n = timezoneLimit(min, max);
}
return n;
}
// Convert number to UTC
private static String numToUTC(double n) {
var hour = (int) n;
var minute = (int) ((n - hour) * 60);
return format("%s%d:%02d", n < 0 ? "GMT-" : "GMT+", hour, minute);
}
// Check out
private static void checkOut() {
if (credit() == 1) {
run();
}
}
}