Android Milliseconds to Date Convert getDateByTimeInMillis(long milliseconds)

Here you can find the source of getDateByTimeInMillis(long milliseconds)

Description

get Date By Time In Millis

Declaration

public static String getDateByTimeInMillis(long milliseconds) 

Method Source Code

//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.annotation.SuppressLint;

public class Main {
    public static Calendar calendar = Calendar.getInstance();
    public static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static String getDateByTimeInMillis(long milliseconds) {
        calendar.setTimeInMillis(milliseconds);
        return dateToString(calendar.getTime());
    }//from  w w  w .  j  a va2s. c o  m

    public static String dateToString(Date date) {
        return dateToString(DEFAULT_DATE_FORMAT, date);
    }

    public static String dateToString(String format, String date) {
        long time = getTimeInMillis(date);
        calendar.clear();
        calendar.setTimeInMillis(time);

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

    public static String dateToString(String format, Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    @SuppressLint("SimpleDateFormat")
    public static long getTimeInMillis(String date) {
        Date currentDate;
        try {
            String format = "HH:mm";
            if (date.contains("-")) {
                format = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat dateForamt = new SimpleDateFormat(format);
            currentDate = dateForamt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();

            currentDate = new Date();
        }
        calendar.setTime(currentDate);
        return calendar.getTimeInMillis();
    }
}

Related

  1. millToDate(String mill)