Попробуйте это:
public static void main(String[] args) {
boolean safetyNet = false;
Scanner in = new Scanner(System.in);
Date d = new Date();
SimpleDateFormat simpFormat = new SimpleDateFormat("hh:mm");
while (!safetyNet) {
System.out.println("Please enter the time in hours and minutes: ");
String timeToString = in.next();
try {
d = simpFormat.parse(timeToString);
safetyNet = true;
String sHours = timeToString.substring(0, 2);
String sMinutes = timeToString.substring(3);
int hours = Integer.parseInt(sHours);
int minutes = Integer.parseInt(sMinutes);
String time=getTimeName(hours, minutes);
if(time.isEmpty()){
safetyNet = false;
}else{
System.out.printf("Time is: " + getTimeName(hours, minutes));
}
} catch (ParseException e) {
System.out.println("Error! Try again: ");
// e.printStackTrace();
}
}
in.close();
}
public static String getTimeName(int hours, int minutes) {
String timeName = "";
if (hours >= 1 && hours <= 12 && minutes >= 0 && minutes <= 59) {
String hourBank[] = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen", "Twenty", "Twenty one", "Twenty two", "Twenty three", "Twenty four", "Twenty five",
"Twenty six", "Twenty seven", "Twenty eight", "Twenty nine" };
if (minutes == 0) {
timeName = hourBank[hours] + " O'clock";
}
else if (minutes == 15) {
timeName = "A quarter past " + hourBank[hours];
}
else if (minutes == 30) {
timeName = "Half Past " + hourBank[hours];
}
else if (hours == 12 && minutes == 45) {
timeName = "A quarter to " + hourBank[hours - 11];
}
else if (minutes == 45) {
timeName = "A quarter to " + hourBank[hours + 1];
}
else if (minutes <= 29) {
timeName = hourBank[minutes] + " minutes past " + hourBank[hours];
}
else if (hours == 12 && minutes >= 31 && minutes <= 59) {
timeName = hourBank[60 - minutes] + " minutes to " + hourBank[hours - 11];
}
else if (minutes >= 31 && minutes <= 60) {
timeName = hourBank[60 - minutes] + " minutes to " + hourBank[hours + 1];
} else {
timeName = "That's not the correct format!";
}
}else{
System.out.println("minutes are between 0 and 60 and hours are between 0 and 12");
}
return timeName;
}