Android examples for java.util:Millisecond
Formats time in milliseconds based on users Android OS preferences (AM/PM vs 24-hour).
//package com.java2s; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; import android.content.Context; public class Main { /**// www . j a v a 2s . co m * Formats time in milliseconds based on users AndroidOS preferences (AM/PM vs 24-hour). * @param milliSeconds Timestamp in milliseconds. * @param context Context entity. * @return Human-readable formatted time string. */ public static String getFormatedTime(Long milliSeconds, Context context) { DateFormat formatter = getFormatter(context); Calendar time = Calendar.getInstance(); time.setTimeInMillis(milliSeconds); return formatter.format(time.getTime()); } /** * Gets android formatter based on current Context and users AndroidOS preferences (AM/PM vs 24-hour). * @param context Context entity. * @return DateFormat entity with formatting set. */ private static DateFormat getFormatter(Context context) { String format = android.text.format.DateFormat .is24HourFormat(context) ? "HH:mm" : "hh:mmaa"; return new SimpleDateFormat(format, Locale.getDefault()); } }