Android Calendar to Timestamp Convert convertToTimeStamp(Calendar time, boolean showSeconds)

Here you can find the source of convertToTimeStamp(Calendar time, boolean showSeconds)

Description

convert To Time Stamp

Parameter

Parameter Description
time a parameter
showSeconds Wheather or not to show just the hours and minutes part, or to show the seconds part also.

Return

The Date in the format: hh:mm:ss

Declaration

private static String convertToTimeStamp(Calendar time,
        boolean showSeconds) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /** Convert date to short time string 
     * @return The Date in the format: hh:mm:ss  
     *///from  w ww  .java2 s.  c om
    public static String convertToTimeStamp(long time) {
        return convertToTimeStamp(time, true);
    }

    /** Convert date to short time string 
     * @return The Date in the format: hh:mm:ss  
     */
    public static String convertToTimeStamp(Date date) {
        return convertToTimeStamp(date, true);
    }

    /** Convert date to short time string
     * @return The Date in the format: hh:mm:ss  
     */
    public static String convertToTimeStamp(Calendar cal) {
        return convertToTimeStamp(cal, true);
    }

    /** Convert date to short time string 
     * @param showSeconds Wheather or not to show just the hours and minutes part, or to show the seconds part also.
     * @return The Date in the format: hh:mm:ss  
     */
    public static String convertToTimeStamp(long time, boolean showSeconds) {
        return convertToTimeStamp(new Date(time), showSeconds);
    }

    /** Convert date to short time string 
     * @param showSeconds Wheather or not to show just the hours and minutes part, or to show the seconds part also.
     * @return The Date in the format: hh:mm:ss  
     */
    public static String convertToTimeStamp(Date date, boolean showSeconds) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return convertToTimeStamp(c, showSeconds);
    }

    /**
     * @param time 
     * @param showSeconds Wheather or not to show just the hours and minutes part, or to show the seconds part also.
     * @return The Date in the format: hh:mm:ss
     */
    private static String convertToTimeStamp(Calendar time,
            boolean showSeconds) {
        String hours = Integer.toString(time.get(Calendar.HOUR_OF_DAY));
        if (hours.length() == 1) {
            hours = '0' + hours;
        }
        String minutes = Integer.toString(time.get(Calendar.MINUTE));
        if (minutes.length() == 1) {
            minutes = '0' + minutes;
        }
        if (showSeconds) {
            String seconds = Integer.toString(time.get(Calendar.SECOND));
            if (seconds.length() == 1) {
                seconds = '0' + seconds;
            }
            return hours + ":" + minutes + ":" + seconds;
        } else {
            return hours + ":" + minutes;
        }
    }
}

Related

  1. Calendar2Timestamp(Calendar c)
  2. convertToTimeStamp(Calendar cal)
  3. nowAsTimestamp()