package com.sogouchat.util; import java.util.Calendar; import android.text.format.Time; public class DateFormatHelper { public static final int DAY_S = 24*60*60*1000; public static final String YESTERDAY = "昨天"; public static final String TDBY = "前天"; public static final String MONDAY= "周一"; public static final String TUESDAY= "周二"; public static final String WEDNESDAY= "周三"; public static final String THURSDAY= "周四"; public static final String FRIDAY= "周五"; public static final String SATURDAY= "周六"; public static final String SUNDAY= "周日"; public static final String LASTMONDAY= "上周一"; public static final String LASTTUESDAY= "上周二"; public static final String LASTWEDNESDAY= "上周三"; public static final String LASTTHURSDAY= "上周四"; public static final String LASTFRIDAY= "上周五"; public static final String LASTSATURDAY= "上周六"; public static final String LASTSUNDAY= "上周日"; public static String FormatDate(long tm){ String str = ""; String strDate=""; Time tmNow = new Time(); tmNow.setToNow(); Time tmItem = new Time(); tmItem.set(tm); String strTime = tmItem.format("%H:%M"); int nWeekDays = 0; //int nWeekDays = 6- tmNow.weekDay+1; if (tmNow.weekDay==0) { nWeekDays = 7; }else { nWeekDays = tmNow.weekDay; } nWeekDays-=1; int nLastWeekDays =nWeekDays+7; //int nDays = tmNow.yearDay-tmItem.yearDay; tmNow.hour = 0; tmNow.minute = 0; tmNow.second = 0; tmItem.hour = 0; tmItem.minute = 0; tmItem.second = 0; int nDays = (int) ((tmNow.toMillis(true)-tmItem.toMillis(true))/DAY_S); if (tmItem.year != tmNow.year) { strDate = tmItem.format("%y-%m-%d"); str = strDate; }else { strDate = tmItem.format("%m-%d"); str = strDate; } if (2<nDays && nDays<=nWeekDays) { switch (tmItem.weekDay) { case 1: str = MONDAY; break; case 2: str = TUESDAY; break; case 3: str = WEDNESDAY; break; case 4: str = THURSDAY; break; case 5: str = FRIDAY; break; case 6: str = SATURDAY; break; case 0: str = SUNDAY; break; default: break; } } if (nWeekDays<nDays && nDays<=nLastWeekDays) { switch (tmItem.weekDay) { case 1: str = LASTMONDAY; break; case 2: str = LASTTUESDAY; break; case 3: str = LASTWEDNESDAY; break; case 4: str = LASTTHURSDAY; break; case 5: str = LASTFRIDAY; break; case 6: str = LASTSATURDAY; break; case 0: str = SUNDAY; break; default: break; } } switch (nDays) { case 0: str = strTime; break; case 1: str = YESTERDAY+strTime; break; case 2: str = TDBY + strTime; break; default: break; } return str; } }