日期工具类

package com.che.lovecar.ui.home;

import com.che.lovecar.support.bean.Order;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 日期工具类
 * <p>
 * 作者:余天然 on 2016/10/13 下午1:18
 */
public class DateUtil {

    public static final String[] weeks = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    public static final SimpleDateFormat weekDataFormat = new SimpleDateFormat("EEEE");
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static final int SECONDS_IN_DAY = 60 * 60 * 24;
    public static final long MILLIS_IN_DAY = 1000L * SECONDS_IN_DAY;

    /**
     * 判断两个时间是不是同一天
     */
    public static boolean isSameDay(final long ms1, final long ms2) {
        final long interval = ms1 - ms2;
        return interval < MILLIS_IN_DAY
                && interval > -1L * MILLIS_IN_DAY
                && toDay(ms1) == toDay(ms2);
    }

    private static long toDay(long millis) {
        return (millis + TimeZone.getDefault().getOffset(millis)) / MILLIS_IN_DAY;
    }

    /**
     * 获取周几
     *
     * @param time
     * @return
     */
    public static int getWeekIndex(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));

        int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekIndex < 0) {
            weekIndex = 0;
        }
        return weekIndex;
    }

    /**
     * 获取周几
     *
     * @param time
     * @return
     */
    public static String getWeekString(long time) {
        return weeks[getWeekIndex(time)];
    }

    /**
     * 发布时间
     * <p>
     * 0、1分钟内:刚刚
     * 1、10分钟内:1分钟前、2分钟前……10分钟前
     * 2、10~60分钟内:20分钟前、30分钟前……
     * 3、大于60分:2016/02/12 11:15
     */
    public static String getOffsetString(long time) {
        long curTime = System.currentTimeMillis();
        long offset = curTime - time;
        long minute = 1000 * 60;

        //1分钟内
        if (offset < minute) {
            return "刚刚";
        }
        //10分钟内
        else if (offset < minute * 10) {
            long var = offset / minute;
            return var + "分钟前";
        }
        //10~60分钟内
        else if (offset < minute * 60) {
            long var = offset / (minute * 10);
            return var + "0分钟前";
        }
        //大于60分
        else {
            return dateFormat.format(time);
        }
    }

    /**
     * 预约时间
     */
    public static String getPreTime(Order item) {
        long preTime = item.getTime();
        long curTime = System.currentTimeMillis();
        String var = "(" + getWeekString(preTime) + ")" + " " + item.getAmOrPm();
        return DateUtil.isSameDay(preTime, curTime) ? "今天" + var : DateUtil.isSameDay(preTime, curTime + MILLIS_IN_DAY) ? "明天" + var : simpleDateFormat.format(preTime);
    }
}
2016-10-28 13:319