Determines if we're in the morning or afternoon - Java java.util

Java examples for java.util:AM PM

Description

Determines if we're in the morning or afternoon

Demo Code


//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(decodePeriod(time));
    }/*from   ww w  .  j  a va2  s .  c o m*/

    public static final String AM = "in the morning";
    public static final String PM = "in the afternoon";
    public static final String EVE = "in the evening";

    /**
     * Determines if we're in the morning or afternoon
     * 
     * @param time the time to decode.
     * @return String representing the period of the day.
     */
    public static String decodePeriod(final Calendar time) {
        String decodedPeriod = null;
        int hour = time.get(Calendar.HOUR_OF_DAY);
        if (hour >= 18) {
            decodedPeriod = EVE;
        } else if (hour >= 12) {
            decodedPeriod = PM;
        } else {
            decodedPeriod = AM;
        }
        return decodedPeriod;
    }
}

Related Tutorials