Here you can find the source of formatTime(int msec, String format)
Parameter | Description |
---|---|
msec | the miliseconds (current time can be get by 'new java.util.Date().getTime()') |
format | the display format (format used from java.text.SimpleDateFormat!) |
public static String formatTime(int msec, String format)
//package com.java2s; /*/*www . j a va 2 s.c o m*/ This file belongs to the Servoy development and deployment environment, Copyright (C) 1997-2010 Servoy BV This program is free software; you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program; if not, see http://www.gnu.org/licenses or write to the Free Software Foundation,Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 */ import java.text.SimpleDateFormat; import java.util.Date; import java.util.SimpleTimeZone; public class Main { /** * Format a given number of miliseconds in a formatted time * * Note:if the time (in milliseconds) is smaller than ~month, it is calulated without a timezone) * * @param msec the miliseconds (current time can be get by 'new java.util.Date().getTime()') * @param format the display format (format used from java.text.SimpleDateFormat!) * @return the formatted time * @see java.text.SimpleDateFormat */ public static String formatTime(int msec, String format) { return formatTime((long) msec, format); } /** * Format a given number of miliseconds in a formatted time * * Note:if the time (in milliseconds) is smaller than ~month, it is calulated without a timezone) * * @param msec the miliseconds (current time can be get by 'new java.util.Date().getTime()') * @param format the display format * @return the formatted time * @see java.text.SimpleDateFormat */ public static String formatTime(long msec, String format) { Date d = new Date(msec); SimpleDateFormat sdf = new SimpleDateFormat(format == null ? "yyyy.MM.dd G 'at' hh:mm:ss z" : format); //$NON-NLS-1$ if (msec < 2678400000L && msec >= 0) //if smaller than a ~month { //format the time without timezone (GMT +0) //now it is possible to format for example telephone calling seconds to a formatted time (hh:mm:ss) //otherwise the timezone is involed sdf.setTimeZone(new SimpleTimeZone(0, "GMT")); //$NON-NLS-1$ } return sdf.format(d); } }