Gets hour and minute out of the calendar and converts it into a String. - Java java.util

Java examples for java.util:Minute

Description

Gets hour and minute out of the calendar and converts it into a String.

Demo Code


//package com.java2s;
import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar calendar = Calendar.getInstance();
        System.out.println(getCalendarTimeAsString(calendar));
    }/*ww w.  ja v a  2 s  .  c  o m*/

    /**
     * Gets hour and minute out of the calendar and converts it into a String.
     * @param calendar object, where you want the time from.
     * @return String of the time.
     */
    public static String getCalendarTimeAsString(Calendar calendar) {
        String timeString = "";
        timeString += calendar.get(Calendar.HOUR_OF_DAY);
        timeString += ":";
        int minute = calendar.get(Calendar.MINUTE);
        if (minute < 10) {
            timeString += "0" + minute;
        } else {
            timeString += minute;
        }

        return timeString;
    }
}

Related Tutorials