From 1970 epoch time in seconds to a formatted time string - Android java.util

Android examples for java.util:Second

Description

From 1970 epoch time in seconds to a formatted time string

Demo Code


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Random;
import android.graphics.Color;

public class Main{
    /**//from  w  w w  . j a v a  2  s. com
     * From 1970 epoch time in seconds to a formatted time string
     * 
     * @param timeInMs
     * @param context
     * @return time in a formatted string
     */
    public static String parseTime(long timeInMs) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(timeInMs);
        SimpleDateFormat dateFormat;
        dateFormat = new SimpleDateFormat(Globals.TIME_FORMAT, Locale.US);
        System.out.println("parsetime function" + calendar.getTime());

        // modulus to round the inputted time to the nearest 15 minutes
        int unroundedMinutes = calendar.get(Calendar.MINUTE);
        int mod = unroundedMinutes % 15;
        calendar.add(Calendar.MINUTE, mod < 8 ? -mod : (15 - mod));
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

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

Related Tutorials