Java Calendar Calculate getTimeString(Calendar cal)

Here you can find the source of getTimeString(Calendar cal)

Description

get Time String

License

Open Source License

Declaration

public static String getTimeString(Calendar cal) 

Method Source Code


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

public class Main {
    public static String getTimeString(Calendar cal) {

        int hour = cal.get(Calendar.HOUR);
        int minute = cal.get(Calendar.MINUTE);
        boolean isAm = (cal.get(Calendar.AM_PM) == Calendar.AM);

        String hourStr;//from   w  w  w  .j  a  v  a 2s  .co  m
        String minuteStr;

        if (!isAm) {
            hour += 12;
        }

        if (hour < 10) {
            hourStr = "0" + Integer.toString(hour);
        } else {
            hourStr = Integer.toString(hour);
        }
        if (minute < 10) {
            minuteStr = "0" + Integer.toString(minute);
        } else {
            minuteStr = Integer.toString(minute);
        }

        String timeStr = hourStr + ":" + minuteStr;
        return timeStr;
    }
}

Related

  1. FormatTime(Calendar p_time, boolean p_showMilliseconds)
  2. getCalendar(long millis)
  3. getGregorianCalendar(String yyyymmdd)
  4. getLongTime(Calendar cal)
  5. getTimeStr3(Calendar timeInstance)
  6. getTimeStrWithMillisecond(Calendar timeInstance)
  7. isAfterWorkTime(Calendar start)
  8. isInWorkTime(Calendar start)
  9. isSameInstant(Calendar cal1, Calendar cal2)