-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathProblem6.java
More file actions
58 lines (52 loc) · 1.94 KB
/
Problem6.java
File metadata and controls
58 lines (52 loc) · 1.94 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
package io.zipcoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Problem6 {
//Step 1: convert given 12 hour numerical format into 24 hours format
//use SimpleDateFormat to format input to 24 hours
String[] units = {"Zero", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty"};
public String convertTo24Hours(String input) {
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
Date date = null;
try {
date = parseFormat.parse(input);
} catch (ParseException e) {
e.printStackTrace();
}
return displayFormat.format(date);
}
//Step 2: convert the 24 hour format into military time phrase in string format
//after the first two digit, add "Hundred and" to the end
//add Hours at the end of each string
public String convertToMilitaryTime(String input){
String formattedInput = convertTo24Hours(input);
String[] time= formattedInput.split(":");
String hour = convertToWords(time[0]);
String minute=convertToWords(time[1]);
return hour + " Hundred and " + minute + " Hours";
}
public String convertToWords(String input){
int num = Integer.parseInt(input);
String word="";
if(num<10){
word+=units[0]+" "+units[num];
}
if(num<20 && num>=10){
word+=units[num];
}
if(num>=20){
if(num%10==0){
word+=tens[num/10];
}
else {
word += tens[num / 10] + " " + units[num % 10];
}
}
return word;
}
}