Convert time in milliseconds to string with format yyyy-MM-dd HH:mm:ss. - Android java.util

Android examples for java.util:Date Format

Description

Convert time in milliseconds to string with format yyyy-MM-dd HH:mm:ss.

Demo Code

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main{

    /**/* www .  ja v  a2  s. c o  m*/
     * Convert time in milliseconds to string with format yyyy-MM-dd HH:mm:ss.
     * 
     * @param milis Milliseconds
     * 
     * @return String of time
     */
    public static String timeMilisToString(long milis) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();

        calendar.setTimeInMillis(milis);

        return sd.format(calendar.getTime());
    }
    /**
     * Convert time in milliseconds to string with format yyyy-MM-dd HH:mm:ss.
     * 
     * @param milis Milliseconds
     * 
     * @return String of time
     */
    public static String timeMilisToString(long milis, String format) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();

        calendar.setTimeInMillis(milis);

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

}

Related Tutorials