Java Calendar Time toTimeString(final Calendar calendar)

Here you can find the source of toTimeString(final Calendar calendar)

Description

Show the Time value of a Calendar as a String.

License

Open Source License

Parameter

Parameter Description
calendar a parameter

Return

String

Declaration


public static String toTimeString(final Calendar calendar) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.util.Calendar;

public class Main {
    /***********************************************************************************************
     * Show the Time value of a Calendar as a String.
     * This is putting right more problems with Java Dates...
     */*from w  w  w. j  a  v  a  2s  .  c o m*/
     * @param calendar
     *
     * @return String
     */

    public static String toTimeString(final Calendar calendar) {
        String strHour;
        String strMinute;
        String strSecond;

        strHour = "";
        strMinute = "";
        strSecond = "";

        if (calendar != null) {
            if (calendar.get(Calendar.HOUR_OF_DAY) < 10) {
                strHour = "0" + calendar.get(Calendar.HOUR_OF_DAY);
            } else {
                strHour = Integer.toString(calendar
                        .get(Calendar.HOUR_OF_DAY));
            }

            if (calendar.get(Calendar.MINUTE) < 10) {
                strMinute = "0" + calendar.get(Calendar.MINUTE);
            } else {
                strMinute = Integer.toString(calendar.get(Calendar.MINUTE));
            }

            if (calendar.get(Calendar.SECOND) < 10) {
                strSecond = "0" + calendar.get(Calendar.SECOND);
            } else {
                strSecond = Integer.toString(calendar.get(Calendar.SECOND));
            }
        }

        return (strHour + ":" + strMinute + ":" + strSecond);
    }
}

Related

  1. timestamp17ToCalendar(String timestamp17String)
  2. timestamp2Calendar(long timestamp)
  3. timestampToCalendar(String timestamp)
  4. toDefaultTimeZone(Calendar calendar)
  5. toTimeStamp(Calendar cal)
  6. toUnixTime(Calendar timestamp)
  7. toVonTime(final Calendar cal)
  8. unixToCalendar(long unixTime)
  9. zeroTime(Calendar cal)