Java Long Number to Date format(long time, SimpleDateFormat format)

Here you can find the source of format(long time, SimpleDateFormat format)

Description

format

License

Apache License

Declaration

public static String format(long time, SimpleDateFormat format) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class Main {
    private static ThreadLocal<SimpleDateFormat> simpleTimeFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override/*  w ww . j  a  va  2  s  .c om*/
        protected SimpleDateFormat initialValue() {

            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
        }
    };

    public static String format(long time, SimpleDateFormat format) {

        if (format == null) {
            return formatDateTime(time);
        }

        Calendar calendar = getCalendar(time);
        return format.format(calendar.getTime());
    }

    public static String formatDateTime(long time) {

        SimpleDateFormat format = getTimeFormat();
        Calendar calendar = getCalendar(time);

        return format.format(calendar.getTime());
    }

    public static Calendar getCalendar() {

        return Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    }

    public static Calendar getCalendar(long time) {

        Calendar calendar = getCalendar();
        calendar.setTimeInMillis(time);
        return calendar;
    }

    public static SimpleDateFormat getTimeFormat() {

        return simpleTimeFormatThreadLocal.get();
    }
}

Related

  1. format(Long date, String pattern)
  2. format(long date, String pattern)
  3. format(long dateInMillis, String pattern)
  4. format(Long dateMs, String type)
  5. format(Long dateTmie, String formate)
  6. formatDate(final long date)
  7. formatDate(final long ticks, final SimpleDateFormat format)
  8. formatDate(final long time)
  9. formatDate(final long timestamp)