美文网首页Android札记技术文Android静水流深
Android小工具之智能格式化时间

Android小工具之智能格式化时间

作者: SoloHo | 来源:发表于2015-04-17 16:54 被阅读3703次

    看着别人的应用的时间显示着“刚刚”、“2分钟之前”、“下午 2:00”是不是很羡慕?但是常常因为项目时间紧凑,没空去扣这些细节的你一定很想要一个这样的小工具啦,这里就送大家一个这样的格式化时间小工具:
    Gist点这里

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.Locale;
    
    /**
     * Created by solo on 15/4/11.
     */
    public class DateUtils {
    
        public static String formatDateTime(Date date) {
            String text;
            long dateTime = date.getTime();
            if (isSameDay(dateTime)) {
                Calendar calendar = GregorianCalendar.getInstance();
                if (inOneMinute(dateTime, calendar.getTimeInMillis())) {
                    return "刚刚";
                } else if (inOneHour(dateTime, calendar.getTimeInMillis())) {
                    return String.format("%d分钟之前", Math.abs(dateTime - calendar.getTimeInMillis()) / 60000);
                } else {
                    calendar.setTime(date);
                    int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
                    if (hourOfDay > 17) {
                        text = "晚上 hh:mm";
                    } else if (hourOfDay >= 0 && hourOfDay <= 6) {
                        text = "凌晨 hh:mm";
                    } else if (hourOfDay > 11 && hourOfDay <= 17) {
                        text = "下午 hh:mm";
                    } else {
                        text = "上午 hh:mm";
                    }
                }
            } else if (isYesterday(dateTime)) {
                text = "昨天 HH:mm";
            } else if (isSameYear(dateTime)) {
                text = "M月d日 HH:mm";
            } else {
                text = "yyyy-M-d HH:mm";
            }
    
            // 注意,如果使用android.text.format.DateFormat这个工具类,在API 17之前它只支持adEhkMmszy
            return new SimpleDateFormat(text, Locale.CHINA).format(date);
        }
    
        private static boolean inOneMinute(long time1, long time2) {
            return Math.abs(time1 - time2) < 60000;
        }
    
        private static boolean inOneHour(long time1, long time2) {
            return Math.abs(time1 - time2) < 3600000;
        }
    
        private static boolean isSameDay(long time) {
            long startTime = floorDay(Calendar.getInstance()).getTimeInMillis();
            long endTime = ceilDay(Calendar.getInstance()).getTimeInMillis();
            return time > startTime && time < endTime;
        }
    
        private static boolean isYesterday(long time) {
            Calendar startCal;
            startCal = floorDay(Calendar.getInstance());
            startCal.add(Calendar.DAY_OF_MONTH, -1);
            long startTime = startCal.getTimeInMillis();
    
            Calendar endCal;
            endCal = ceilDay(Calendar.getInstance());
            endCal.add(Calendar.DAY_OF_MONTH, -1);
            long endTime = endCal.getTimeInMillis();
            return time > startTime && time < endTime;
        }
    
        private static boolean isSameYear(long time) {
            Calendar startCal;
            startCal = floorDay(Calendar.getInstance());
            startCal.set(Calendar.MONTH, Calendar.JANUARY);
            startCal.set(Calendar.DAY_OF_MONTH, 1);
            return time >= startCal.getTimeInMillis();
        }
    
        private static Calendar floorDay(Calendar startCal) {
            startCal.set(Calendar.HOUR_OF_DAY, 0);
            startCal.set(Calendar.MINUTE, 0);
            startCal.set(Calendar.SECOND, 0);
            startCal.set(Calendar.MILLISECOND, 0);
            return startCal;
        }
    
        private static Calendar ceilDay(Calendar endCal) {
            endCal.set(Calendar.HOUR_OF_DAY, 23);
            endCal.set(Calendar.MINUTE, 59);
            endCal.set(Calendar.SECOND, 59);
            endCal.set(Calendar.MILLISECOND, 999);
            return endCal;
        }
    
    }
    

    代码很简单,一看就懂的。

    转载请附上本文地址

    相关文章

      网友评论

      • 呃哈哈:Gist不行啊
      • jkyeo:写的不错,刚好需要这样的工具。

      本文标题:Android小工具之智能格式化时间

      本文链接:https://www.haomeiwen.com/subject/zzjefttx.html