And qq time format template instance details, and time format
Go directly to the Code with comments in the code!
/*** Convert a timestamp into a prompt time string. For example, * 2 minutes-24 hours HH: mm * Yesterday HH: mm * day before yesterday HH: mm * MM: dd hh: mm * last year MM: dd hh: mm * Previous year MM: dd hh: mm * farther yyyy: MM: mm: dd hh: mm * millisecond calculation * @ param charttime * @ return */public static String convertChatDetailTimeFormat (long charttime) {long curTime = System. currentTimeMillis (); long time = curTime-charttime; XCApplication. base_log. I (XCConfig. TAG_SYSTEM_OUT, time + "--- time difference" + time/1000/60 + "Minute"); XCApplication. base_log. I (XCConfig. TAG_SYSTEM_OUT, curTime + "--- current time" + format (new Date (curTime), FORMAT_LONG_CN_1); XCApplication. base_log. I (XCConfig. TAG_SYSTEM_OUT, charttime + "--- chartTime" + format (new Date (charttime), FORMAT_LONG_CN_1); if (time <120*1000 & time> = 0) {return "just";} else if (time> = 120*1000 & time <3600*24*1000) {return format (new Date (charttime), FORMAT_HH_MM );} else if (time> = 3600*24*1*1000 & time <3600*24*2*1000) {return "yesterday" + format (new Date (charttime ), FORMAT_HH_MM);} else if (time> = 3600*24*2*1000 & time <3600*24*3*1000) {return "the day before yesterday" + format (new Date (charttime), FORMAT_HH_MM );} else if (time> = 3600*24*3*1000 & time <3600*24*365*1*1000) {return format (new Date (charttime ), FORMAT_MM_DD_HH_MM);} else if (time> = 3600*24*365*1*1000 & time <3600*24*365*2*1000) {return "last year" + format (new Date (charttime), FORMAT_MM_DD_HH_MM );} else if (time> = 3600*24*365*2*1000 & time <3600*24*365*3*1000) {return "Previous Year" + format (new Date (charttime), FORMAT_MM_DD_HH_MM);} else if (time> = 3600*24*365*3*1000) {return format (new Date (charttime), FORMAT_LONG_CN_1) ;}else {return "just ";}}
There is a small problem here, that is, the natural day time spans the actual day time, there may be yesterday's time does not show yesterday, but is displayed as HH: mm, so the test came to the door, change the requirement. Change the condition from 2 minutes to 24 hours to 2 minutes to within today.
The requirement here is changed
* No display within 2 minutes
* 2 minutes-Today HH: mm
* Yesterday HH: mm
* HH: mm the day before yesterday
* MM: dd hh: mm this year
* Last Year, MM: dd hh: mm
* Year before MM: dd hh: mm
* Farther away: yyyy: MM: dd hh: mm
This is not a big problem. The question is how the cross-year situation should be. The message received three minutes before. 001, that is, the message displayed as yesterday or last year. If the receiving time of information is longer than the time, how to display it.
After some hard work, I finally finalized it. Here, I want to modify the product again and ask the product to be the final version.
/*** Ultimate method ** converts a timestamp into a prompt time string. For example, * 2 minutes-2 minutes-2 minutes-HH: mm * Yesterday HH: mm * the day before yesterday HH: mm * this year MM: dd hh: mm * last year MM: dd hh: mm * the year before MM: dd hh: mm * farther yyyy: MM: dd hh: mm * millisecond calculation * @ param time * @ return */public static String convertWEChartTimeFormatFinalMethed (long time) {long curTime = System. currentTimeMillis (); String showTimeFormat = ""; long temp = curTime-time; if (temp <120*1000 && Temp> = 0) {showTimeFormat = ""; return showTimeFormat;} Date mayTime = new Date (time); // Date today = UtilDate. parse ("02:02:02. 001 ", UtilDate. FORMAT_FULL); Date today = new Date (); // Time Value String mayTime_FORMAT_SHORT = format (mayTime, FORMAT_SHORT); String mayTime_FORMAT_SHORT_YEAR = getYear (mayTime); if (mayTime. after (today) {// showTimeFormat = format (mayTime, FORMAT_LONG_CN_1) ;} Else {if (mayTime_FORMAT_SHORT! = Null &&! Maytime_format_assist.trim (). toString (). equals ("") {// today's time yyyy-MM-dd String today_str = format (today, FORMAT_SHORT); String thisYear_str = getYear (today ); // Yesterday's time yyyy-MM-dd Calendar calLastDay = Calendar. getInstance (); calLastDay. setTime (today); calLastDay. add (Calendar. DAY_OF_YEAR,-1); System. out. println ("yesterday:" + format (calLastDay. getTime (), FORMAT_SHORT); String lastDay = format (calLastDay. getTime (), FORMAT_SHORT); // The time of the day before yesterday, yyyy-MM-dd Calendar calpreviusday = Calendar ar. getInstance (); calpreviusday. setTime (today); calpreviusday. add (Calendar. DAY_OF_YEAR,-2); System. out. println ("day before yesterday:" + format (calpreviusday. getTime (), FORMAT_SHORT); String previusday = format (calpreviusday. getTime (), FORMAT_SHORT); // last year, yyyy Calendar calLastYear = Calendar. getInstance (); calLastYear. setTime (today); calLastYear. add (Calendar. YEAR,-1); String lastYear = getYear (calLastYear. getTime (); System. out. println ("last year:" + format (calLastYear. getTime (), FORMAT_SHORT); // the previous year's time yyyy Calendar calpreviusyear = Calendar ar. getInstance (); calpreviusyear. setTime (today); calpreviusyear. add (Calendar. YEAR,-2); String previousYear = getYear (calpreviusyear. getTime (); System. out. println ("previous year:" + format (calpreviusyear. getTime (), FORMAT_SHORT); // first, judge whether it is today if (maytime_format_0000.equals (today_str) {// today, it is displayed as showTimeFormat = format (mayTime, FORMAT_HH_MM );} else if (maytime_format_0000.equals (lastDay) {// Yesterday showTimeFormat = "yesterday" + format (mayTime, FORMAT_HH_MM);} else if (maytime_format_0000.equals (previusday )) {// showTimeFormat = "day before yesterday" + format (mayTime, FORMAT_HH_MM);} else if (second (thisYear_str) {// showTimeFormat = format (mayTime, FORMAT_MM_DD_HH_MM) this year );} else if (lastYear) {// last year showTimeFormat = "last year" + format (mayTime, FORMAT_MM_DD_HH_MM);} else if (mayTime_FORMAT_SHORT_YEAR.equals (previusyear )) {// showTimeFormat = "Previous Year" + format (mayTime, FORMAT_MM_DD_HH_MM);} else {// showTimeFormat = format (mayTime, FORMAT_LONG_CN_1); }}} return showTimeFormat ;}
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!