android開發中即時聊天軟體中時間的獲得

來源:互聯網
上載者:User

標籤:

`/**     * 根據long類型的時間戳記,轉換為一個String類型的描述性時間     * 通話記錄如果發生在今天:“15:30”     * 發生在昨天:“昨天8:23”     * 發生在前天:“前天4:56”     * 更早:     “2016/04/15”     * @param timeStample     * @return     */      //timeStample是聊天記錄發生的時間    public static String getTime(long timeStample) {        //得到現在的時間戳記        long now=System.currentTimeMillis();        //在java中,int類型的數進行除法運算,只能的整數,正是利用這一點,        //在下列日期中,只要沒過昨天24點,無論相差了1s還是23小時,除法得到的結果都是前一天,        int day=(int) (now/1000/60/60/60-timeStample/1000/60/60/60);                switch (day) {        //如果是0這則說明是今天,顯示時間        case 0:SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");        return sdf.format(timeStample);        //如果是1說明是昨天,顯示昨天+時間        case 1:SimpleDateFormat sdf1=new SimpleDateFormat("HH:mm");        return "昨天"+sdf1.format(timeStample);        //如果是1說明是前天,顯示前天+時間        case 2:SimpleDateFormat sdf2=new SimpleDateFormat("HH:mm");        return  "前天"+sdf2.format(timeStample);        //結果大於2就只顯示年月日        default:SimpleDateFormat sdf3=new SimpleDateFormat("yyyy:MM:dd");        return sdf3.format(timeStample);

android開發中即時聊天軟體中時間的獲得

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.