2 * Copyright 2001-2003 Sun Microsystems, Inc.
4 * See the file "license.terms" for information on usage and
5 * redistribution of this file, and for a DISCLAIMER OF ALL
8 import java.util.regex.Pattern;
10 public class TimeUtils {
13 * Returns a phrase that conveys the exactness
16 * @param hour the hour of the time
17 * @param min the minute of the time
19 * @return a string phrase
21 private static String timeApprox(int hour, int min) {
31 return "a little after";
38 * Returns a phrase that conveys the minutes in relation
41 * @param hour the hour of the time
42 * @param min the minute of the time
44 * @return a string phrase.
46 private static String timeMin(int hour, int min) {
62 } else if (mm == 10) {
64 } else if (mm == 15) {
65 return "quarter past";
66 } else if (mm == 20) {
68 } else if (mm == 25) {
69 return "twenty-five past";
70 } else if (mm == 30) {
72 } else if (mm == 35) {
73 return "twenty-five to";
74 } else if (mm == 40) {
76 } else if (mm == 45) {
78 } else if (mm == 50) {
80 } else if (mm == 55) {
88 * Returns a phrase that conveys the hour in relation
91 * @param hour the hour of the time
92 * @param min the minute of the time
94 * @return a string phrase.
96 private static String timeHour(int hour, int min) {
100 if (min > 32) { // PBL: fixed from flite_time
112 } else if (hh == 1) {
114 } else if (hh == 2) {
116 } else if (hh == 3) {
118 } else if (hh == 4) {
120 } else if (hh == 5) {
122 } else if (hh == 6) {
124 } else if (hh == 7) {
126 } else if (hh == 8) {
128 } else if (hh == 9) {
130 } else if (hh == 10) {
132 } else if (hh == 11) {
134 } else if (hh == 12) {
142 * Returns a phrase that conveys the time of day.
144 * @param hour the hour of the time
145 * @param min the minute of the time
147 * @return a string phrase
149 private static String timeOfDay(int hour, int min) {
157 } else if (hh > 17) {
158 return "in the evening";
159 } else if (hh > 11) {
160 return "in the afternoon";
161 } else if ((hh == 0) && (min < 33)) {
164 return "in the morning";
169 * Returns a string that corresponds to the given time.
171 * @param time the time in the form HH:MM
173 * @return the time in string, null if the given time is not in the
176 public static String timeToString(String time) {
177 String theTime = null;
178 if (Pattern.matches("[012][0-9]:[0-5][0-9]", time)) {
179 int hour = Integer.parseInt(time.substring(0, 2));
180 int min = Integer.parseInt(time.substring(3));
182 theTime = timeToString(hour, min);
188 * Returns a string that corresponds to the given time.
190 * @param hour the hour
191 * @param min the minutes
193 * @return the time in string, null if the given time out of range
195 public static String timeToString(int hour, int min) {
196 String theTime = "The time is now, " +
197 timeApprox(hour, min) + " " +
198 timeMin(hour, min) + " " +
199 timeHour(hour, min) + ", " +
200 timeOfDay(hour, min) + "." ;