Java Hour Format formatTime(int msec, String format)

Here you can find the source of formatTime(int msec, String format)

Description

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)

License

Open Source License

Parameter

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!)

Return

the formatted time

Declaration

public static String formatTime(int msec, String format) 

Method Source Code

//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);
    }
}

Related

  1. formatTime(Date time)
  2. formatTime(double s)
  3. formatTime(final Calendar cal)
  4. formatTime(final Date date)
  5. formatTime(final long time)
  6. formatTime(java.util.Date date)
  7. formatTime(long aTime)
  8. formatTime(long ms)
  9. formatTime(long time)