File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Example using the String.format method.
3+ */
4+ public class Format {
5+
6+ /**
7+ * Returns a time string in 12-hour format.
8+ *
9+ * @param hour between 0 and 23
10+ * @param minute between 0 and 59
11+ */
12+ public static String timeString (int hour , int minute ) {
13+ String ampm ;
14+ if (hour < 12 ) {
15+ ampm = "AM" ;
16+ if (hour == 0 ) {
17+ hour = 12 ; // midnight
18+ }
19+ } else {
20+ ampm = "PM" ;
21+ hour = hour - 12 ;
22+ }
23+ return String .format ("%02d:%02d %s" , hour , minute , ampm );
24+ }
25+
26+ public static void main (String [] args ) {
27+ System .out .println (timeString (0 , 0 ));
28+ System .out .println (timeString (7 , 30 ));
29+ System .out .println (timeString (12 , 5 ));
30+ System .out .println (timeString (23 , 59 ));
31+ }
32+
33+ }
You can’t perform that action at this time.
0 commit comments