Java Long Number to Date format(long date, String pattern)

Here you can find the source of format(long date, String pattern)

Description

Format a date/time into a specific pattern.

License

Open Source License

Parameter

Parameter Description
date The date to format expressed in milliseconds.
pattern The pattern to use to format the date.

Return

The formatted date.

Declaration

public static String format(long date, String pattern) 

Method Source Code


//package com.java2s;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.TimeZone;

public class Main {
    /**//from ww  w .j  ava2  s  .  c  om
     *  Format a date/time into a specific pattern.
     *
     *  @param  date    The date to format expressed in milliseconds.
     *  @param  pattern The pattern to use to format the date.
     *  @return The formatted date.
     */
    public static String format(long date, String pattern) {
        return format(new Date(date), pattern);
    }

    /**
     *  Format a date/time into a specific pattern.
     *
     *  @param  date    The date to format expressed in milliseconds.
     *  @param  pattern The pattern to use to format the date.
     *  @return The formatted date.
     */
    public static String format(Date date, String pattern) {
        DateFormat df = createDateFormat(pattern);
        return df.format(date);
    }

    /**
     *  Return a date format set to GMT time zone.
     *
     *  @param  pattern  Tthe pattern used for date/time formatting.
     *  @return The configured format for this pattern.
     */
    private static DateFormat createDateFormat(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        TimeZone gmt = TimeZone.getTimeZone("GMT");
        sdf.setTimeZone(gmt);
        sdf.setLenient(true);
        return sdf;
    }
}

Related

  1. dateToString(long date)
  2. dateToString(long date, String formate)
  3. dateToStrLong(java.util.Date dateDate, String format)
  4. format(long date)
  5. format(Long date, String pattern)
  6. format(long dateInMillis, String pattern)
  7. format(Long dateMs, String type)
  8. format(Long dateTmie, String formate)
  9. format(long time, SimpleDateFormat format)