Android Long to Date Convert convertToTimeStamp(long time)

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

Description

Convert date to short time string

Return

The Date in the format: hh:mm:ss

Declaration

public static String convertToTimeStamp(long time) 

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