日期

package com.cfae.cfaeapp.utils;

import android.annotation.SuppressLint;

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

/**
 * Created by 郝悅 on 2019/1/29.
 */

public class DateTimeUtils {
    public static String QQFormatTime(long time) {
        Date date = new Date();
        date.setTime(time);
        if (isSameYear(date)) { //同一年 顯示MM-dd HH:mm
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);
            if (isSameDay(date)) { //同一天 顯示HH:mm
                int minute = minutesAgo(time);
                if (minute < 60) {//1小時以內 顯示n分鐘前
                    if (minute <= 1) {//一分鐘以內,顯示剛剛
                        return "剛剛";
                    } else {
                        return minute + "分鐘前";
                    }
                } else {
                    return simpleDateFormat.format(date);
                }
            } else {
                if (isYesterday(date)) {//昨天,顯示昨天+HH:mm
                    return "昨天 " + simpleDateFormat.format(date);
                } else if (isSameWeek(date)) {//本週,顯示周幾+HH:mm
                    String weekday = null;
                    if (date.getDay() == 1) {
                        weekday = "週一";
                    }
                    if (date.getDay() == 2) {
                        weekday = "週二";
                    }
                    if (date.getDay() == 3) {
                        weekday = "週三";
                    }
                    if (date.getDay() == 4) {
                        weekday = "週四";
                    }
                    if (date.getDay() == 5) {
                        weekday = "週五";
                    }
                    if (date.getDay() == 6) {
                        weekday = "週六";
                    }
                    if (date.getDay() == 0) {
                        weekday = "週日";
                    }
                    return weekday + " " + simpleDateFormat.format(date);
                } else {//同一年 顯示MM-dd HH:mm
                    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA);
                    return sdf.format(date);
                }
            }
        } else {//不是同一年 顯示完整日期yyyy-MM-dd HH:mm
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
            return sdf.format(date);
        }
    }

    /**
     * 幾分鐘前
     *
     * @param time
     * @return
     */
    public static int minutesAgo(long time) {
        return (int) ((System.currentTimeMillis() - time) / (60000));
    }


    /**
     * 與當前時間是否在同一周
     * 先判斷是否在同一年,而後根據Calendar.DAY_OF_YEAR判斷所得的週數是否一致
     *
     * @param date
     * @return
     */
    public static boolean isSameWeek(Date date) {
        if (isSameYear(date)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int a = calendar.get(Calendar.DAY_OF_YEAR);

            Date now = new Date();
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(now);
            int b = calendar1.get(Calendar.DAY_OF_WEEK);
            return a == b;
        } else {
            return false;
        }
    }

    /**
     * 是不是當前時間的昨天
     * 獲取指定時間的後一天的日期,判斷與當前日期是不是同一天
     *
     * @param date
     * @return
     */
    public static boolean isYesterday(Date date) {
        Date yesterday = getNextDay(date, 1);
        return isSameDay(yesterday);
    }

    /**
     * 判斷與當前日期是不是同一天
     *
     * @param date
     * @return
     */
    public static boolean isSameDay(Date date) {
        return isEquals(date, "yyyy-MM-dd");
    }

    /**
     * 判斷與當前日期是不是同一月
     *
     * @param date
     * @return
     */
    public static boolean isSameMonth(Date date) {
        return isEquals(date, "yyyy-MM");
    }

    /**
     * 判斷與當前日期是不是同一年
     *
     * @param date
     * @return
     */
    public static boolean isSameYear(Date date) {
        return isEquals(date, "yyyy");
    }


    /**
     * 格式化Date,判斷是否相等
     *
     * @param date
     * @return 是返回true,不是返回false
     */
    private static boolean isEquals(Date date, String format) {
        //當前時間
        Date now = new Date();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sf = new SimpleDateFormat(format);
        //獲取今天的日期
        String nowDay = sf.format(now);
        //對比的時間
        String day = sf.format(date);
        return day.equals(nowDay);
    }

    /**
     * 獲取某個date第n天的日期
     * n<0 表示前n天
     * n=0 表示當天
     * n>1 表示後n天
     *
     * @param date
     * @param n
     * @return
     */
    public static Date getNextDay(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, n);
        date = calendar.getTime();
        return date;
    }

    //     string類型轉換爲long類型
// strTime要轉換的String類型的時間
// formatType時間格式
    // strTime的時間格式和formatType的時間格式必須相同
    public static long stringToLong(String strTime, String formatType) throws ParseException {
        Date date = stringToDate(strTime, formatType); // String類型轉成date類型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date類型轉成long類型
            return currentTime;
        }
    }

    // date類型轉換爲long類型
    // date要轉換的date類型的時間
    public static long dateToLong(Date date) {
        return date.getTime();
    }

    // string類型轉換爲date類型
// strTime要轉換的string類型的時間,formatType要轉換的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
// HH時mm分ss秒,
// strTime的時間格式必需要與formatType的時間格式相同
    public static Date stringToDate(String strTime, String formatType) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }
}
相關文章
相關標籤/搜索