Java Timestamp Format formatTimeFromTimestamp(long ts, String fmt)

Here you can find the source of formatTimeFromTimestamp(long ts, String fmt)

Description

Format timestamp ts to string of format fmt, in UTC, for example, yyyy-MM-dd HH:mm:ss

License

Apache License

Parameter

Parameter Description
ts a parameter
fmt a parameter

Return

If failed, return empty string ""

Declaration

public static String formatTimeFromTimestamp(long ts, String fmt) 

Method Source Code

//package com.java2s;
/*//ww w . j av a2  s .c  o m
 * Copyright 2015, Yahoo Inc.
 * Copyrights licensed under the Apache License.
 * See the accompanying LICENSE file for terms.
 */

import java.util.Date;

import java.util.TimeZone;

public class Main {
    /**
     * Format timestamp ts to string of format fmt, in UTC, for example, yyyy-MM-dd HH:mm:ss
     * @param ts
     * @param fmt
     * @return If failed, return empty string ""
     */
    public static String formatTimeFromTimestamp(long ts, String fmt) {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(fmt);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            return sdf.format(new Date(ts));
        } catch (Exception ex) {

        }
        return "";
    }
}

Related

  1. formattedDateTime(long timestamp)
  2. formatTime(Date aTs_Datetime)
  3. formatTime(Date date)
  4. formatTime(Timestamp time, boolean onlyTime)
  5. formatTime(Timestamp ts, Locale locale)
  6. formatTimestamp(Date date)
  7. formatTimestamp(Date timestamp)
  8. formatTimestamp(Date timestamp)
  9. formatTimestamp(Date timestamp)