Determines which quarter of the hour the supplied minute is closest to. - Java java.util

Java examples for java.util:Minute

Description

Determines which quarter of the hour the supplied minute is closest to.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int minute = 2;
        System.out.println(decodeQuarterHour(minute));
    }//from  ww  w.ja va  2 s.co 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";

    /**
     * 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;
    }
}

Related Tutorials