Java Timestamp Format formatTimestampTime(Timestamp ts, boolean colon)

Here you can find the source of formatTimestampTime(Timestamp ts, boolean colon)

Description

format Timestamp Time

License

Open Source License

Parameter

Parameter Description
ts a parameter
colon a parameter

Declaration

public static String formatTimestampTime(Timestamp ts, boolean colon) 

Method Source Code

//package com.java2s;
/*/*from w w w . j  av a  2s  .  com*/
 * Created on Oct 23, 2003
 *
 * Copyright (c) 2005 Derone Bryson
 *
 * This program is free software; you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation; either version 2, 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 General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this software; see the file COPYING. If not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * As a special exception, Derone Bryson and the StopMojo Project gives 
 * permission for additional uses of the text contained in its release of 
 * StopMojo.
 *
 * The exception is that, Derone Bryson and the the StopMojo Project hereby 
 * grants permission for non-GPL compatible modules (jar files, libraries, 
 * codecs, etc.) to be used and distributed together with StopMojo. This 
 * permission is above and beyond the permissions granted by the GPL license 
 * StopMojo is covered by.
 *
 * This exception does not however invalidate any other reasons why the 
 * executable file might be covered by the GNU General Public License.
 *
 * This exception applies only to the code released by Derone Bryson and/or the
 * StopMojo Project under the name StopMojo. If you copy code from other Free 
 * Software Foundation releases into a copy of StopMojo, as the General Public 
 * License permits, the exception does not apply to the code that you add in 
 * this way. To avoid misleading anyone as to the status of such modified files, 
 * you must delete this exception notice from them.
 *
 * If you write modifications of your own for StopMojo, it is your choice 
 * whether to permit this exception to apply to your modifications. If you do 
 * not wish that, delete this exception notice.  
 */

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;

public class Main {
    private static NumberFormat nf = NumberFormat.getInstance();

    /**
     * 
     * @param ts
     * @param colon
     * @return
     */
    public static String formatTimestampTime(Timestamp ts, boolean colon) {
        String tstr;

        Calendar c = Calendar.getInstance();

        c.setTimeInMillis(ts.getTime());

        int hours = c.get(Calendar.HOUR_OF_DAY);
        if (hours > 12)
            hours -= 12;
        if (hours == 0)
            hours = 12;
        tstr = formatNumber("00", hours);
        if (colon)
            tstr += ":";
        tstr += formatNumber("00", c.get(Calendar.MINUTE));

        if (c.get(Calendar.HOUR_OF_DAY) < 12)
            tstr += "a";
        else
            tstr += "p";

        return tstr;
    }

    /**
     * 
     * @param ts
     * @return
     */
    public static String formatTimestampTime(Timestamp ts) {
        return formatTimestampTime(ts, true);
    }

    /**
     * Formats a number using {@link DecimalFormat}.
     * 
     * @param Format
     * @param Num
     * @return
     */
    public static String formatNumber(String Format, int Num) {
        if (nf instanceof DecimalFormat)
            ((DecimalFormat) nf).applyPattern(Format);

        return nf.format(Num);
    }

    /**
     * Formats a number using {@link DecimalFormat}.
     * 
     * @param Format
     *            the format to apply as defined by DecimalFormat
     * @param Num
     *            the number to be formatted
     * @return the formatted number
     */
    public static String formatNumber(String Format, double Num) {
        if (nf instanceof DecimalFormat)
            ((DecimalFormat) nf).applyPattern(Format);

        return nf.format(Num);
    }
}

Related

  1. formatTimestamp(Timestamp timestamp)
  2. formatTimestamp(Timestamp timestamp, int iType)
  3. formatTimestamp(Timestamp timestamp, String pattern)
  4. formatTimestamp(Timestamp ts, int dateStyle, int timeStyle, Locale locale)
  5. formatTimeStampMsec(long epochTime)
  6. formatTimestampToIndex(java.sql.Timestamp timestamp)
  7. formatTimestampToLong(final String timestampStr)
  8. formatTimestampWithSlashes( java.sql.Timestamp tsZeitpunkt)
  9. formatTimeToString(long timestamp)