Android Long to Date Convert formatDateTime(long timeToFormatInMilliseconds)

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

Description

format Date Time

Declaration

public static String formatDateTime(long timeToFormatInMilliseconds) 

Method Source Code

//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;
import android.content.Context;

public class Main {
    public static String formatDateTime(Context context, String timeToFormat) {

        String finalDateTime = "";

        SimpleDateFormat iso8601Format = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");

        Date date = null;//from w ww  .j  a  v  a 2  s  . co  m
        if (timeToFormat != null) {
            try {
                date = iso8601Format.parse(timeToFormat);
            } catch (Exception e) {
                date = null;
            }

            if (date != null) {
                long when = date.getTime();
                int flags = 0;
                flags |= android.text.format.DateUtils.FORMAT_SHOW_TIME;
                flags |= android.text.format.DateUtils.FORMAT_SHOW_DATE;
                flags |= android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
                flags |= android.text.format.DateUtils.FORMAT_SHOW_YEAR;

                finalDateTime = android.text.format.DateUtils
                        .formatDateTime(context, when
                                + TimeZone.getDefault().getOffset(when),
                                flags);
            }
        }
        return finalDateTime;
    }

    public static String formatDateTime(long timeToFormatInMilliseconds) {

        /*SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        Date date = iso8601Format.parse(timeToFormatInMilliseconds);*/

        SimpleDateFormat formatter = new SimpleDateFormat(
                "MM/dd/yyyy hh:mm:ss a");

        formatter.setTimeZone(TimeZone.getDefault());
        String currentDate = formatter.format(timeToFormatInMilliseconds);
        return currentDate;

    }
}

Related

  1. formatElapsedTime(long totalTimeSeconds)
  2. formatSecondsToHHMM(long seconds)
  3. getStringByFormat(long milliseconds, String format)
  4. getStringByFormat(long milliseconds, String format)
  5. convertToTime(long time)
  6. getElapsed(long start, long end)