Here you can find the source of formatTimestampLong(long timestamp, String datepattern, TimeZone tz)
Parameter | Description |
---|---|
timestamp | long type timestamp, unit:second |
datepattern | a given date pattern |
tz | TimeZone |
public static String formatTimestampLong(long timestamp, String datepattern, TimeZone tz)
//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; } }