android格式化時間中文版,Android 仿微信聊天時間格式化顯示功能

本文給大家分享android仿微信聊天時間格式化顯示功能。

在同一年的顯示規則:

如果是當天顯示格式為 HH:mm 例:14:45

如果是昨天,顯示格式為 昨天 HH:mm 例:昨天 13:12

如果是在同一周 顯示格式為 周一 HH:mm 例:周一14:05

如果不是同一周則顯示格式為 M月d日 早上或者其它 HH:mm 例: 2月5日 早上10:10

不在同一年的顯示規則:

顯示格式為 yyyy年M月d日 晚上或者其它 HH:mm 例:2016年2月5日 晚上18:05

代碼中如果有誤,請留言。

代碼實現如下:

import Java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class Test {

public static void main(String[] args) {

System.out.println("當前時間:"+new SimpleDateFormat("yyyy/M/d HH:mm:ss").format(System.currentTimeMillis()));

System.out.println("2016/2/1 05:05:00 顯示為:"+getNewChatTime(1454666700000l));

System.out.println("2017/2/1 05:05:00 顯示為:"+getNewChatTime(1485983100000l));

System.out.println("2017/2/4 12:05:00 顯示為:"+getNewChatTime(1486181100000l));

System.out.println("2017/2/5 10:10:00 顯示為:"+getNewChatTime(1486260600000l));

System.out.println("2017/2/5 13:12:00 顯示為:"+getNewChatTime(1486271520000l));

System.out.println("2017/2/6 14:05:00 顯示為:"+getNewChatTime(1486361100000l));

/*運行結果:

當前時間:2017/2/9 14:36:36

2016/2/1 05:05:00 顯示為:2016年2月5日 晚上18:05

2017/2/1 05:05:00 顯示為:2月2日 凌晨05:05

2017/2/4 12:05:00 顯示為:2月4日 中午12:05

2017/2/5 13:12:00 顯示為:2月5日 早上10:10

2017/2/5 13:12:00 顯示為:2月5日 下午13:12

2017/2/6 14:05:00 顯示為:周一14:05*/

}

/**

* 時間戳格式轉換

*/

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

public static String getNewChatTime(long timesamp) {

String result = "";

Calendar todayCalendar = Calendar.getInstance();

Calendar otherCalendar = Calendar.getInstance();

otherCalendar.setTimeInMillis(timesamp);

String timeFormat="M月d日 HH:mm";

String yearTimeFormat="yyyy年M月d日 HH:mm";

String am_pm="";

int hour=otherCalendar.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 = todayCalendar.get(Calendar.YEAR)==otherCalendar.get(Calendar.YEAR);

if(yearTemp){

int todayMonth=todayCalendar.get(Calendar.MONTH);

int otherMonth=otherCalendar.get(Calendar.MONTH);

if(todayMonth==otherMonth){//表示是同一個月

int temp=todayCalendar.get(Calendar.DATE)-otherCalendar.get(Calendar.DATE);

switch (temp) {

case 0:

result = getHourAndMin(timesamp);

break;

case 1:

result = "昨天 " + getHourAndMin(timesamp);

break;

case 2:

case 3:

case 4:

case 5:

case 6:

int dayOfMonth = otherCalendar.get(Calendar.WEEK_OF_MONTH);

int todayOfMonth=todayCalendar.get(Calendar.WEEK_OF_MONTH);

if(dayOfMonth==todayOfMonth){//表示是同一周

int dayOfWeek=otherCalendar.get(Calendar.DAY_OF_WEEK);

if(dayOfWeek!=1){//判斷當前是不是星期日 如想顯示為:周日 12:09 可去掉此判斷

result = dayNames[otherCalendar.get(Calendar.DAY_OF_WEEK)-1] + getHourAndMin(timesamp);

}else{

result = getTime(timesamp,timeFormat);

}

}else{

result = getTime(timesamp,timeFormat);

}

break;

default:

result = getTime(timesamp,timeFormat);

break;

}

}else{

result = getTime(timesamp,timeFormat);

}

}else{

result=getYearTime(timesamp,yearTimeFormat);

}

return result;

}

/**

* 當天的顯示時間格式

* @param time

* @return

*/

public static String getHourAndMin(long time) {

SimpleDateFormat format = new SimpleDateFormat("HH:mm");

return format.format(new Date(time));

}

/**

* 不同一周的顯示時間格式

* @param time

* @param timeFormat

* @return

*/

public static String getTime(long time,String timeFormat) {

SimpleDateFormat format = new SimpleDateFormat(timeFormat);

return format.format(new Date(time));

}

/**

* 不同年的顯示時間格式

* @param time

* @param yearTimeFormat

* @return

*/

public static String getYearTime(long time,String yearTimeFormat) {

SimpleDateFormat format = new SimpleDateFormat(yearTimeFormat);

return format.format(new Date(time));

}

}

以上所述是小編給大家介紹的Android 仿微信聊天時間格式化顯示功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/397413.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/397413.shtml
英文地址,請注明出處:http://en.pswp.cn/news/397413.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java分享第十七天-01(封裝操作xml類)

做自動化測試的人&#xff0c;都應該對XPATH很熟悉了&#xff0c;但是在用JAVA解析XML時&#xff0c;我們通常是一層層的遍歷進去&#xff0c;這樣的代碼的局限性很大&#xff0c;也不方便&#xff0c;于是我們結合一下XPATH&#xff0c;來解決這個問題。所需要的JAR包&#xf…

Ubuntu12.04 內核樹建立

先查看自己使用的內核版本 linlin-virtual-machine:~$ uname -r 3.2.0-23-generic 如果安裝系統時&#xff0c;自動安裝了源碼。在 /usr/src 目錄下有對應的使用的版本目錄。 linlin-virtual-machine:~$ cd /usr/src linlin-virtual-machine:/usr/src$ ls linux-headers-3.2.0…

【mysql】Innodb三大特性之double write

1、doublewrite buffer&#xff08;mysql官方的介紹&#xff09; InnoDB uses a novel file flush technique called doublewrite. Before writing pages to the data files, InnoDB first writes them to a contiguous area called the doublewrite buffer. Only after the wr…

android crop 大圖,com.android.camera.action.CROP 實現圖片剪裁

APP 中選取圖片之后&#xff0c;有時候需要進行剪裁&#xff0c;比如頭像。以下是啟動代碼。在我的項目中&#xff0c;傳的是 filePath&#xff0c;所以我轉了一下&#xff0c;但實際上從相冊選擇圖片后&#xff0c;用 data.getData() 就可獲得 uri。Uri uri Uri.fromFile(new…

Who Gets the Most Candies? POJ - 2886 (線段樹)

按順時針給出n個小孩&#xff0c;n個小孩每個人都有一個紙&#xff0c;然后每個人都有一個val&#xff0c;這個val等于自己的因子數&#xff0c;如果這個val是正的&#xff0c;那就順時針的第val個孩子出去&#xff0c;如果是負的話&#xff0c;就逆時針的第val個孩子出去&…

javax.validation.ValidationException: Unable to find a default provider

2019獨角獸企業重金招聘Python工程師標準>>> [ERROR] [2016-11-16 13:58:21 602] [main] (FrameworkServlet.java:457) Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name org.springframewo…

第十章練習題----2

package com.Hanqi2;public class xitizhuhanshu {public static void main(String[] args) {// TODO Auto-generated method stubxiti tm new xiti("黑色","15寸");xitizhs tm3 new xitizhs("藍色","15寸");tm.Call("654"…

關于微信“被返回頁”在被返回時自動刷新

網上有很多這些文章&#xff0c;但我覺得沒一篇真正解決這個問題&#xff0c;倒是能給人一個解決方案的思路&#xff0c;對&#xff0c;就是posState事件。 要解決這個問題也不難&#xff0c;使用history的replaceState屬性替換當前網頁鏈接&#xff08;其實作用是在不增加hist…

android視頻播放器api,03.視頻播放器Api說明

03.視頻播放器Api說明目錄介紹01.最簡單的播放02.如何切換視頻內核03.切換視頻模式04.切換視頻清晰度05.視頻播放監聽06.列表中播放處理07.懸浮窗口播放08.其他重要功能Api09.播放多個視頻10.VideoPlayer相關Api11.Controller相關Api12.邊播放邊緩存api13.類似抖音視頻預加載14…

使用Python重命名MP3標簽

從Window復制MP3文件的到Ubuntu下&#xff0c;MP3標簽很多是亂碼。于是想自己寫個Python程序處理一下。 從酷狗復制過來的音樂文件名都是“作者 - 標題”&#xff0c;所以可以通過解析文件名直接獲取作者和標題信息。 需要下載eyeD3模塊 $ sudo apt-get install python-eyed3 代…

Taurus.MVC 2.0 開源發布:WebAPI開發教程

背景&#xff1a; 有用戶反映&#xff0c;Tausus.MVC 能寫WebAPI么&#xff1f; 能&#xff01; 教程呢&#xff1f; 嗯&#xff0c;木有&#xff01; 好吧&#xff0c;剛好2.0出來&#xff0c;就帶上WEBAPI教程了&#xff01; 開源地址&#xff1a; https://github.com/cyq116…

android 鎖屏 home,android 鎖屏界面禁用長按home 和menu(recent apps)

android 5.1 系統中public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {//檢查當前是否鎖屏&#xff0c; 可以添加getTopApp()判斷當前activity 來屏蔽2398 final boolean keyguardOn keyguardOn();添加新的方法&#xff1a;//獲…

Chrome瀏覽器調試踩坑

Chrome瀏覽器若在響應式狀態下&#xff0c;頁面縮放比例不是100%&#xff0c;元素會“竄位”&#xff0c;點擊元素會點擊到元素周圍的元素 Chrome頁面縮放比例不為100%時&#xff0c;table的單元格就算沒有邊框&#xff08;CSS去掉了&#xff09;也會顯示出邊框&#xff08;縫隙…

WordPress 博客文章時間格式the_time()設置

國外設計的WordPress 主題里的文章的時間格式是類似“十一月 21, 2010”這種格式的&#xff0c;而中國人習慣的是年在前&#xff0c;月緊跟其后&#xff0c;日在末尾&#xff0c;所以看國外的就顯得很別扭&#xff0c;但是我們可以通過修改WP時間代碼來設置成為我們中國人習慣的…

linux yum

更改linux YUM源方法&#xff1a;第一步&#xff1a;進入yum配置文件目錄&#xff1a;cd /etc/yum.repos.d/第二步&#xff1a;備份配置文件&#xff1a;mv CentOS-Base.repo CentOS-Base.repo.bak第三步&#xff1a;下載網易的配置&#xff08;或其他源配置文件&#xff09;&a…

chrome瀏覽器去掉緩存的方法

方法一&#xff1a; 1.開發說打開開發者工具 勾選這個訪問可以 方法二: commandshiftR 轉載于:https://www.cnblogs.com/kaibindirver/p/9378572.html

Apache Tomcat目錄下各個文件夾的作用

1.bin&#xff1a;存放各種不同平臺開啟與關閉Tomcat的腳本文件。 2.lib&#xff1a;存tomcat與web應用的Jar包。 3.conf&#xff1a;存放tomcat的配置文件。 4.webapps&#xff1a;web應用的發布目錄。 5.work&#xff1a;tomcat把由各種jsp生成的servlet文件存放的地方。 6.l…

sony z2 android 5.0,索尼Xperia Z2 5.0 root教程_索尼Z2獲取5.0系統的root

來說一下咱們的索尼Xperia Z2手機的5.0系統的root&#xff0c;因為現在很多機友的系統是5.0的&#xff0c;可是對于5.0的系統很多機友還不知道如何進行root操作&#xff0c;之前的針對4.4的系統的root方法肯定是用不到5.0的系統上的&#xff0c;因此需要專門的針對5.0的root軟件…

ABP文檔 - Javascript Api - AJAX

本節內容&#xff1a; AJAX操作相關問題ABP的方式 AJAX 返回信息處理錯誤 HTTP 狀態碼WrapResult和DontWrapResult特性 Asp.net Mvc 控制器Asp.net Web Api 控制器動態Web Api層Asp.net Core 控制器動態Web Api層AJAX操作相關問題 執行一個AJAX調用在現在的應用里非常常見&…

視達配色教程17 灰色的色彩意象是什么

視達配色教程17 灰色的色彩意象是什么 一、總結 一句話總結&#xff1a;沒有個性的色彩 1、灰色的一般意象是什么&#xff1f; 所有混沌的情感不友好的色彩可怕、恐怖和殘忍感情貧乏或者內向年齡和年老遺忘的過去貧困與謙虛劣等的顏色秘密與非法合適的中等-男式時裝的標準 二、…