聊天時間戳格式轉(zhuǎn)換

static String dayNames[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

/**
 * 時間戳格式轉(zhuǎn)換
 */
public static String getNewChatTime(long timestamp) {
    String result = "";

    Calendar curCalendar = Calendar.getInstance();
    Calendar showCalendar = Calendar.getInstance();
    showCalendar.setTimeInMillis(timestamp);

    String timeFormat;
    String yearTimeFormat;
    String am_pm = "";
    int hour = showCalendar.get(Calendar.HOUR_OF_DAY);
    if (hour >= 0 && hour < 6) {
        am_pm = "凌晨";
    } else if (hour >= 6 && hour < 12) {
        am_pm = "早上";
    } else if (hour == 12) {
        am_pm = "中午";
    } else if (hour > 12 && hour < 18) {
        am_pm = "下午";
    } else if (hour >= 18) {
        am_pm = "晚上";
    }
    timeFormat = "M月d日 " + am_pm + "HH:mm";
    yearTimeFormat = "yyyy年M月d日 " + am_pm + "HH:mm";

    boolean yearTemp = curCalendar.get(Calendar.YEAR) == showCalendar.get(Calendar.YEAR);
    if (yearTemp) {
        int curMonth = curCalendar.get(Calendar.MONTH);
        int showMonth = showCalendar.get(Calendar.MONTH);
        // 表示是同一個月
        if (curMonth == showMonth) {
            int temp = curCalendar.get(Calendar.DATE) - showCalendar.get(Calendar.DATE);
            switch (temp) {
                case 0:
                    result = getHourAndMin(timestamp);
                    break;
                case 1:
                    result = "昨天 " + getHourAndMin(timestamp);
                    break;
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                    int curDayOfMonth = curCalendar.get(Calendar.WEEK_OF_MONTH);
                    int showDayOfMonth = showCalendar.get(Calendar.WEEK_OF_MONTH);
                    // 表示是同一周
                    if (showDayOfMonth == curDayOfMonth) {
                        int dayOfWeek = showCalendar.get(Calendar.DAY_OF_WEEK);
                        //判斷當前是不是星期日     如想顯示為:周日 12:09 可去掉此判斷
                        if (dayOfWeek != 1) {
                            result = dayNames[showCalendar.get(Calendar.DAY_OF_WEEK) - 1]
                                    + getHourAndMin(timestamp);
                        } else {
                            result = getTime(timestamp, timeFormat);
                        }
                    } else {
                        result = getTime(timestamp, timeFormat);
                    }
                    break;
                default:
                    result = getTime(timestamp, timeFormat);
                    break;
            }
        } else {
            result = getTime(timestamp, timeFormat);
        }
    } else {
        result = getYearTime(timestamp, yearTimeFormat);
    }
    return result;
}

/**
 * 當天的顯示時間格式
 *
 * @param time time
 * @return HH:mm
 */
public static String getHourAndMin(long time) {
    SimpleDateFormat format = new SimpleDateFormat("HH:mm");
    return format.format(new Date(time));
}

/**
 * 不同一周的顯示時間格式
 *
 * @param time       time
 * @param timeFormat timeFormat
 * @return String
 */
public static String getTime(long time, String timeFormat) {
    SimpleDateFormat format = new SimpleDateFormat(timeFormat);
    return format.format(new Date(time));
}

/**
 * 不同年的顯示時間格式
 *
 * @param time           time
 * @param yearTimeFormat yearTimeFormat
 * @return String
 */
public static String getYearTime(long time, String yearTimeFormat) {
    SimpleDateFormat format = new SimpleDateFormat(yearTimeFormat);
    return format.format(new Date(time));
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容