public class TimeUtil { /** * 把一个时间戳转为描述性的文字 * 如果时间戳代表的时间是今天14:35 返回“14:35” * 如果时间戳代表的时间是昨天14:35 返回“昨天 14:35” * * 如果时间戳代表的时间是更往前的14:35 返回“xxxx-xx-xx 14:35” * * @param time 秒值 * @return */ public static String getTime(long time) { String result = ""; Calendar calendar = Calendar.getInstance(); int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; //根据time与当前时间的差距计算差了多少天 long now = System.currentTimeMillis();//毫秒值 //计算差了多少天 int day = (int) (now / 1000 / 3600 / 24 - time / 1000 / 3600 / 24); if (week!=0){ int dayCha = week - day; if (day == 0) { result = new SimpleDateFormat("HH:mm").format(time); return result; } if (day == 1) { result = "昨天 " + new SimpleDateFormat("HH:mm").format(time); return result; } if (day >=2) { if (dayCha >= 1 && dayCha <= 6) { String weekTime = getWeekTime(time); result = weekTime; return result; }else { result = new SimpleDateFormat("yyyy-MM-dd").format(time); return result; } } }else { //周日 week=7; int dayCha = week - day; if (day == 0) { result = new SimpleDateFormat("HH:mm").format(time); return result; } if (day == 1) { result = "昨天 " + new SimpleDateFormat("HH:mm").format(time); return result; } if (day >=2) { if (dayCha >= 1 && dayCha <= 6) { String weekTime = getWeekTime(time); result = weekTime; return result; }else { result = new SimpleDateFormat("yyyy-MM-dd").format(time); return result; } } } return result; } private static String getWeekTime(long date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String time = df.format(date); String Week = ""; Calendar c = Calendar.getInstance(); try { c.setTime(df.parse(time)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } int i = c.get(Calendar.DAY_OF_WEEK); if (i== 1) { Week += "星期天"; } if (i ==2) { Week += "星期一"; } if (i ==3) { Week += "星期二"; } if (i == 4) { Week += "星期三"; } if (i == 5) { Week += "星期四"; } if (i == 6) { Week += "星期五"; } if (i == 7) { Week += "星期六"; } return Week; } public static String getMyTime(long time){ Calendar calendar=Calendar.getInstance(); String result = ""; int week =calendar.get(Calendar.DAY_OF_WEEK); //根据time与当前时间的差距计算差了多少天 long now = System.currentTimeMillis();//毫秒值 //计算差了多少天 int day = (int)(now/1000/3600/24-time/1000/3600/24); switch (day) { case 0: result = new SimpleDateFormat("HH:mm").format(time); break; case 1: result = "昨天 "+new SimpleDateFormat("HH:mm").format(time); break; case 2: case 3: case 4: case 5: case 6: //result = "前天 "+new SimpleDateFormat("HH:mm").format(time*1000); String weekTime = getWeekTime(time); result=weekTime; //显示周几 break; default: result = new SimpleDateFormat("yyyy-MM-dd").format(time); break; } return result; } public static String getTimestr(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String time = format.format(date); return time; } public static String getTimeStrWithSec(Date date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = format.format(date); return time; } public static Date stringToDate(String dateString) { ParsePosition position = new ParsePosition(0); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date dateValue = simpleDateFormat.parse(dateString, position); return dateValue; } public static long stringToLong(String dateString) { ParsePosition position = new ParsePosition(0); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date dateValue = simpleDateFormat.parse(dateString, position); return dateValue.getTime(); } public static long getDateTime(String sTime){ SimpleDateFormat format= new SimpleDateFormat("yyyy.MM"); String time=sTime; Date date= null; try { date = format.parse(time); } catch (ParseException e) { e.printStackTrace(); } if (date!=null){ return date.getTime(); } System.out.print("Format To times:"+date.getTime()); return date.getTime(); } public static long getDateTimeForXx(String sTime){ SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time=sTime; Date date= null; try { date = format.parse(time); } catch (ParseException e) { e.printStackTrace(); } if (date!=null){ return date.getTime(); } System.out.print("Format To times:"+date.getTime()); return date.getTime(); } }
对于时间的处理,在社交类的app需求最多。