Android Long to Date Convert convertToTimeStamp(long time, boolean showSeconds)

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

Description

Convert date to short time string

Parameter

Parameter Description
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

public static String convertToTimeStamp(long 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  
     *//*  w  ww . j  a v a 2  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. convToEnglishTime(long dateMsec)
  2. converTime(long timestamp, String[] timeLabels)
  3. convertNotNull(Long long1)
  4. convertToDateStamp(long time)
  5. convertToTimeStamp(long time)
  6. dateFromUnixTimestamp(final long unixTimestamp)
  7. dateFromUtc(long milliseconds)
  8. dateUtil(long date)
  9. daysSinceTimestamp(long timestamp)