Java Hour getHour(boolean military)

Here you can find the source of getHour(boolean military)

Description

Returns the current hour as a string.

License

Open Source License

Return

the current hour.

Declaration

public static String getHour(boolean military) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005-2012 Synopsys, Incorporated
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*www .j  a va 2s.c o m*/
 * Synopsys, Inc - Initial implementation 
 *******************************************************************************/

import java.util.Calendar;

public class Main {
    private static Calendar mCalendar = null;

    /**
     * Returns the current hour as a string. Option specifies whether to use military time. Range is from 1-24.
     * @return the current hour.
     */
    public static String getHour(boolean military) {
        if (mCalendar == null)
            Init();
        String retVal = null;
        int val = mCalendar.get(Calendar.HOUR_OF_DAY);
        if (military) {
            if (val < 10)
                retVal = "0" + val;
            else
                retVal = val + "";
        } else {
            if (val > 12)
                val -= 12;
            if (val < 10)
                retVal = "0" + val;
            else
                retVal = val + "";
        }
        return retVal;
    }

    /**
     * This method creates a new Calendar object
     */
    public static void Init() {
        mCalendar = Calendar.getInstance();
    }
}

Related

  1. getHour()
  2. getHour()
  3. getHour()
  4. getHour()
  5. getHour()
  6. getHour(Date begin, Date end)
  7. getHour(Date d)
  8. getHour(Date d)
  9. getHour(Date d)