Android_時(shí)間轉(zhuǎn)換

截圖.jpg

效果就是“1月21日 晚上23:03” 這種在聊天中的需求

   /**
     * 用在Chat界面
     * 將時(shí)間轉(zhuǎn)換為字符串
     * NEED:
     *  今天
     *  早上10:41 (6 - 12) 21,600,000 - 43,200,000
     *  中午1:00 (12 - 2:30) 43,200,000 - 52,200,000
     *  下午3:00 (2:30-6)52,200,000 - 64,800,000
     *  傍晚7:00 (6-11)64,800,000 - 82,800,000
     *  深夜12:00 (11-12)82,800,000 - 86,400,000
     *  凌晨1:00 (12-6) 0 - 21,600,000
     *
     *  昨天
     *  昨天 早上10:41 (6 - 12)
     *  昨天 中午1:00 (12 - 2:30)
     *  昨天 下午3:00 (2:30-6)
     *  昨天 傍晚7:00 (6-11)
     *  昨天 深夜12:00 (11-12)
     *  昨天 凌晨1:00 (12-6)
     *
     *  前天
     *  周一 早上10:41 (6 - 12)
     *  周一 中午1:00 (12 - 2:30)
     *  周一 下午3:00 (2:30-6)
     *  周一 傍晚7:00 (6-11)
     *  周一 深夜12:00 (11-12)
     *  周一 凌晨1:00 (12-6)
     *
     *  在往后
     *  1月21號 早上10:41 (6 - 12)
     *  1月21號 中午1:00 (12 - 2:30)
     *  1月21號 下午3:00 (2:30-6)
     *  1月21號 傍晚7:00 (6-11)
     *  1月21號 深夜12:00 (11-12)
     *  1月21號 凌晨1:00 (12-6)
     *
     *  這里參考的對象是微信
     *  Other:我這里比微信多做了一層判斷,具體是周幾上午下午這個(gè),微信沒有
     *         我加入了年的判斷,如果是去年的就是 201x年x月x日 14:00
     * @author NEGIER
     * @date 2018/1/24
     */
    public static String timeText(long time) {
        SimpleDateFormat format = new SimpleDateFormat();
        // 獲取今天零點(diǎn)的時(shí)間
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        Date date = null;
        Date yearDate = null;
        try {
            format.applyPattern("yyyy-MM-dd");
            date = format.parse(year +"-"+ month +"-"+day);

            format.applyPattern("yyyy");
            yearDate = format.parse(year+"");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (yearDate.getTime()>time){
            format.applyPattern("yyyy年MM月dd日 HH:mm");
            return format.format(time);
        }

        // 兩個(gè)時(shí)間相減
        long timeInterval = time-date.getTime();
        // 判斷并返回值 《不準(zhǔn)笑話我蹩腳的英文,不準(zhǔn),哼~》
        // 這里有一個(gè)Get:"yyyy-MM-dd hh:mm:ss" hh寫成HH就是24小時(shí)制,hh是12小時(shí)制
        if (timeInterval>0){
            // 凌晨 (00-06)
            final int lincheng = 21600000;
            // 早上 (06-12)
            final int morning = 43200000;
            // 中午 (12-14.30)
            final int noon = 52200000;
            // 下午 (14:30-18)
            final int afternoon = 64800000;
            // 傍晚 (18-23)
            final int bangwan = 82800000;
            // 深夜 (23 - 24)
            final int shengye = 86400000;
            if (timeInterval<=lincheng){
                format.applyPattern("凌晨hh:mm");
            }else if (timeInterval<=morning){
                format.applyPattern("早上hh:mm");
            }else if (timeInterval<=noon){
                format.applyPattern("中午hh:mm");
            }else if (timeInterval<=afternoon){
                format.applyPattern("下午hh:mm");
            }else if (timeInterval<=bangwan){
                format.applyPattern("傍晚hh:mm");
            }else if (timeInterval<=shengye){
                format.applyPattern("深夜hh:mm");
            }
        }else if (timeInterval>-86400000){
            long absTimeInterval = Math.abs(timeInterval);
            // 這里順序和上面相比倒過來了
            // 昨天 深夜 (23 - 24) 3,600,000
            final int shengye = 3600000;
            // 昨天 傍晚 (18-23)21,600,000
            final int bangwan = 21600000;
            // 昨天 下午 (14:30-18)34,200,000
            final int afternoon = 34200000;
            // 昨天 中午 (12-14.30)43,200,000
            final int noon = 43200000;
            // 昨天 早上 (06-12)64,800,000
            final int morning = 64800000;
            // 昨天 凌晨 (00-06)
            final int lincheng = 86400000;

            if (absTimeInterval<=shengye){
                format.applyPattern("昨天 深夜hh:mm");
            }else if (absTimeInterval<=bangwan){
                format.applyPattern("昨天 傍晚hh:mm");
            }else if (absTimeInterval<=afternoon){
                format.applyPattern("昨天 下午hh:mm");
            }else if (absTimeInterval<=noon){
                format.applyPattern("昨天 中午hh:mm");
            }else if (absTimeInterval<=morning){
                format.applyPattern("昨天 早上hh:mm");
            }else if (absTimeInterval<=lincheng){
                format.applyPattern("昨天 凌晨hh:mm");
            }
        }else if (timeInterval>-86400000*2){
            long absTimeInterval = Math.abs(timeInterval);
            // 昨天 深夜 (23 - 24) 3,600,000
            final int shengye = 3600000*2;
            // 昨天 傍晚 (18-23)21,600,000
            final int bangwan = 21600000*2;
            // 昨天 下午 (14:30-18)34,200,000
            final int afternoon = 34200000*2;
            // 昨天 中午 (12-14.30)43,200,000
            final int noon = 43200000*2;
            // 昨天 早上 (06-12)64,800,000
            final int morning = 64800000*2;
            // 昨天 凌晨 (00-06)
            final int lincheng = 86400000*2;

            if (absTimeInterval<=shengye){
                format.applyPattern("E 深夜hh:mm");
            }else if (absTimeInterval<=bangwan){
                format.applyPattern("E 傍晚hh:mm");
            }else if (absTimeInterval<=afternoon){
                format.applyPattern("E 下午hh:mm");
            }else if (absTimeInterval<=noon){
                format.applyPattern("E 中午hh:mm");
            }else if (absTimeInterval<=morning){
                format.applyPattern("E 早上hh:mm");
            }else if (absTimeInterval<=lincheng){
                format.applyPattern("E 凌晨hh:mm");
            }
        }else{
            format.applyPattern("MM月dd日 HH:mm");
        }

        return format.format(time);
    }

結(jié)語

這個(gè)沒有什么難度,只是蠻花時(shí)間的,直接拿去用吧。
這是我自己寫的聊天框架中用到的,也許你也用得著,顧分享。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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