類似新浪微博動態釋出時間轉換機制,顯示剛剛、幾分鐘前、幾小時前、昨天、前天····
阿新 • • 發佈:2019-01-28
經常看到社交類app中關於動態釋出的時間點與當前時間的換算,說麻煩也不麻煩,說簡單也不簡單,只是計算起來有點繞。如以當前時間為基準,自己某個時間在微博上發表一個動態,發表時間提示有多種顯示,如剛剛、幾分鐘前、幾個小時前、昨天、前天、日期等等。自己之前做過類似的時間換算,雖然不是最優,但最終效果還是達到了,下面附上原始碼
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestDate { /** 準備第一個模板,從字串中提取出日期數字 */ private static String pat1 = "yyyy-MM-dd HH:mm:ss" ; /** 準備第二個模板,將提取後的日期數字變為指定的格式*/ private static String pat2 = "yyyy年MM月dd日 HH:mm:ss" ; /** 例項化模板物件*/ private static SimpleDateFormat sdf1 = new SimpleDateFormat(pat1) ; private static SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ; public static void main(String[] args) { Date dates = Dates(); String string = sdf1.format(dates); System.out.println(string); String time = "2013-01-29 19:38:21"; System.out.println(getTime(time)); } public static Long farmatTime(String string){ Date date =null; try { SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); date = Date(sf.parse(string)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return date.getTime(); } public static Date Date(Date date){ Date datetimeDate; datetimeDate = new Date(date.getTime()); return datetimeDate; } public static Date Dates(){ Date datetimeDate; Long dates = 1361515285070L; datetimeDate = new Date(dates); return datetimeDate; } public static String getTime(String commitDate) { // TODO Auto-generated method stub // 在主頁面中設定當天時間 Date nowTime = new Date(); String currDate = sdf1.format(nowTime); Date date = null ; try{ // 將給定的字串中的日期提取出來 date = sdf1.parse(commitDate) ; }catch(Exception e){ e.printStackTrace() ; } int nowDate = Integer.valueOf(currDate.substring(8, 10)); int commit = Integer.valueOf(commitDate.substring(8, 10)); String monthDay = sdf2.format(date).substring(5, 12); String yearMonthDay = sdf2.format(date).substring(0, 12); int month = Integer.valueOf(monthDay.substring(0, 2)); int day = Integer.valueOf(monthDay.substring(3, 5)); if (month < 10 && day <10) { monthDay = monthDay.substring(1, 3) + monthDay.substring(4); }else if(month < 10){ monthDay = monthDay.substring(1); }else if (day < 10) { monthDay = monthDay.substring(0, 3) + monthDay.substring(4); } int yearMonth = Integer.valueOf(yearMonthDay.substring(5, 7)); int yearDay = Integer.valueOf(yearMonthDay.substring(8, 10)); if (yearMonth < 10 && yearDay <10) { yearMonthDay = yearMonthDay.substring(0, 5) + yearMonthDay.substring(6,8) + yearMonthDay.substring(9); }else if(yearMonth < 10){ yearMonthDay = yearMonthDay.substring(0, 5) + yearMonthDay.substring(6); }else if (yearDay < 10) { yearMonthDay = yearMonthDay.substring(0, 8) + yearMonthDay.substring(9); } String str = " 00:00:00"; float currDay = farmatTime(currDate.substring(0, 10) + str); float commitDay = farmatTime(commitDate.substring(0, 10) +str); int currYear = Integer.valueOf(currDate.substring(0, 4)); int commitYear = Integer.valueOf(commitDate.substring(0, 4)); int flag = (int)(farmatTime(currDate)/1000 - farmatTime(commitDate)/1000); String des = null; String hourMin = commitDate.substring(11, 16); int temp = flag; if (temp < 60) { System.out.println("A"); if (commitDay < currDay) { des = "昨天 " + hourMin; }else { des = "剛剛"; } }else if (temp < 60 * 60) { System.out.println("B"); if (commitDay < currDay) { des = "昨天 " + hourMin; }else { des = temp/60 + "分鐘以前"; } }else if(temp< 60*60*24){ System.out.println("C"); int hour = temp / (60*60); if (commitDay < currDay) { des = "昨天 " + hourMin; }else { if (hour < 6) { des = hour + "小時前"; }else { des = hourMin; } } }else if (temp < (60 * 60 * 24 *2 )) { System.out.println("D"); if (nowDate - commit == 1) { des = "昨天 " + hourMin; }else { des = "前天 " + hourMin; } }else if (temp < 60 * 60 * 60 * 3) { System.out.println("E"); if (nowDate - commit == 2) { des = "前天 " + hourMin; }else { if (commitYear < currYear) { des = yearMonthDay + hourMin; }else { des = monthDay + hourMin; } } }else { System.out.println("F"); if (commitYear < currYear) { des = yearMonthDay + hourMin; }else { des = monthDay + hourMin; } } if (des == null) { des = commitDate; } return des; } public static Date Date(){ Date datetimeDate; Long dates = 1361514787384L; datetimeDate = new Date(dates); return datetimeDate; } }
執行結果:
2013-01-29 19:38:21 轉換結果
下圖為新浪關於時間顯示效果圖