Java examples for java.util:Minute
Decodes the minutes into plain language.
//package com.java2s; import java.util.Calendar; public class Main { public static void main(String[] argv) throws Exception { Calendar time = Calendar.getInstance(); System.out.println(decodeMinutes(time)); }/*from w w w . j a v a 2s . c o m*/ public static final String QUARTER_PAST = "a quarter past"; public static final String QUARTER_TO = "a quarter to"; public static final String HALF_PAST = "half past"; public static final String JUST_AFTER = "just after"; public static final String JUST_BEFORE = "just before"; /** * Decodes the minutes into plain language. Rules: 7 minutes after each 15 * is just after, 6 minutes before each 15 is just before, 15 is quarter past, * 30 is half past, 45 is quarter to. * * E.g. 8:43 = just before a quarter to nine. * * @param time the time to decode. * @return String representing the position in the hour. */ public static String decodeMinutes(final Calendar time) { int minute = time.get(Calendar.MINUTE); String decodedMinutes = decodeQuarterHour(minute); for (int i = 0; i <= 45; i += 15) { if (minute > i && minute <= i + 7) { decodedMinutes = JUST_AFTER + " " + decodedMinutes; } if (minute > i + 7 && minute < i + 15) { decodedMinutes = JUST_BEFORE + " " + decodedMinutes; } } return decodedMinutes; } /** * Determines which quarter of the hour the supplied minute is closest to. * Returns an empty string if the answer is the hour. Otherwise it returns * half past, a quarter past or a quarter to. * * @param minute the minute of the hour. * @return String based on the section of the hour the minute is in or empty. */ private static String decodeQuarterHour(int minute) { String decodedMinutes = ""; if (minute > 7 && minute <= 22) { decodedMinutes = QUARTER_PAST + " "; } if (minute > 22 && minute <= 37) { decodedMinutes = HALF_PAST + " "; } if (minute > 37 && minute <= 52) { decodedMinutes = QUARTER_TO + " "; } return decodedMinutes; } }