Java examples for java.util:Hour
Decodes the hour of a supplied time into a plain language representation.
//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(decodeHours(time)); }//from w w w . jav a2 s. c om public static final String[] HOURS = { "midnight", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "noon" }; /** * Decodes the hour of a supplied time into a plain language representation. * Special cases are midnight and noon. Other times are one, two, three etc. * * @param time the time to decode. * @return String representing the hour. */ public static String decodeHours(final Calendar time) { String decodedHour = null; int hour = time.get(Calendar.HOUR_OF_DAY); // If we are in the range of quarter to the hour we will be talking about the next hour if (time.get(Calendar.MINUTE) > 37) { hour = hour + 1; if (hour == 24) { hour = 0; } } // Now convert from 24hr hours > 12 e.g. 13:00 is 1pm so the same as 1 for our purposes if (hour > 12) { hour = hour - 12; } // Now return the value decodedHour = HOURS[hour]; return decodedHour; } }