Java Long Number to Time formatTimestampLong(long timestamp, String datepattern, TimeZone tz)

Here you can find the source of formatTimestampLong(long timestamp, String datepattern, TimeZone tz)

Description

format a timestamp into a given date pattern string

License

Open Source License

Parameter

Parameter Description
timestamp long type timestamp, unit:second
datepattern a given date pattern
tz TimeZone

Return

String

Declaration

public static String formatTimestampLong(long timestamp,
        String datepattern, TimeZone tz) 

Method Source Code

//package com.java2s;
import java.text.DateFormat;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    /**//  w  w  w .  j  a  va2s. c  o  m
     * format a timestamp into a given date pattern string
     * 
     * @param timestamp long type timestamp, unit:second
     * @param datepattern a given date pattern
     * @param tz TimeZone
     * @return String
     */
    public static String formatTimestampLong(long timestamp,
            String datepattern, TimeZone tz) {
        long newTimestamp = timestamp;
        DateFormat formatter = getDateFormat(datepattern, Locale.US, tz);
        newTimestamp = newTimestamp / 1000;
        newTimestamp = newTimestamp * 1000;
        Date date = new Date(newTimestamp);
        return formatter.format(date);
    }

    /**
     * return a standard DateFormat instance, which has a given Local, TimeZone,
     * and with a strict check
     * 
     * @param datepattern String
     * @param locale Locale
     * @param timeZone TimeZone
     * @return DateFormat
     */
    public static DateFormat getDateFormat(String datepattern,
            Locale locale, TimeZone timeZone) {
        DateFormat formatter = new SimpleDateFormat(datepattern, locale);
        formatter.setLenient(false);
        if (timeZone == null) {
            formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        } else {
            formatter.setTimeZone(timeZone);
        }
        return formatter;
    }
}

Related

  1. elapsedTime(long start, long end)
  2. elapsedTimeToString(long elapsedTime)
  3. formattaData(long date)
  4. formatTime(long date)
  5. formatTime(long ms)
  6. formatToUTC(long datetime)
  7. formatXSDateTime(long lastModified)
  8. getAmPm(long time)
  9. getCollectTimeInLong(Date date)