Here you can find the source of convertToTimeStamp(long time, boolean showSeconds)
Parameter | Description |
---|---|
showSeconds | Wheather or not to show just the hours and minutes part, or to show the seconds part also. |
public static String convertToTimeStamp(long time, boolean showSeconds)
//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; } } }