Java Long Number to Date formatDateTime(long time)

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

Description

format Date Time

License

Apache License

Declaration

public static String formatDateTime(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> simpleTimeFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override//from  w  ww . j  a v  a2 s  . c  o m
        protected SimpleDateFormat initialValue() {

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

    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();
    }

    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());
    }
}

Related

  1. formatDateISO(long millis)
  2. formatDateTime(long d)
  3. formatDateTime(long d)
  4. formatDateTime(long date)
  5. formatDateTime(long millis)
  6. formatDateTime(long timestampEpoch)
  7. formatDateTimeStamp(final long timeInMillis, final String dateFormatString)
  8. formateDate(Long date, String format)
  9. formatEpoch(long date)