Java Long Number to Date formatDate(long time)

Here you can find the source of formatDate(long time)

Description

format Date

License

Apache License

Declaration

public static String formatDate(long time) 

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> simpleDateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override//from   w  ww .  ja  v  a 2 s.  c  o  m
        protected SimpleDateFormat initialValue() {

            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };
    private static ThreadLocal<SimpleDateFormat> simpleTimeFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {

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

    public static String formatDate(long time) {

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

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

    public static SimpleDateFormat getDateFormat() {

        return simpleDateFormatThreadLocal.get();
    }

    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 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 SimpleDateFormat getTimeFormat() {

        return simpleTimeFormatThreadLocal.get();
    }
}

Related

  1. formatDate(long ms)
  2. formatDate(long ms)
  3. formatDate(long p_millis)
  4. formatDate(long t)
  5. formatDate(long time)
  6. formatDate(long time)
  7. formatDate(long time)
  8. formatDate(long time, String format)
  9. formatDate(long time_millis)