package com.partynetwork.dataprovider.util; import android.annotation.SuppressLint; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import com.partynetwork.iparty.imessage.OpenfireConstant; /** * 时间工具类 * * @author way * */ @SuppressLint("SimpleDateFormat") public class TimeUtil { private static final String FORMAT = "yyyy-MM-dd HH:mm:ss"; public static String converTime(long time) { long currentSeconds = System.currentTimeMillis() / 1000; long timeGap = currentSeconds - time / 1000;// 与现在时间相差秒数 String timeStr = null; if (timeGap > 3 * 24 * 60 * 60) { timeStr = getDayTime(time) + " " + getMinTime(time); } else if (timeGap > 24 * 2 * 60 * 60) {// 2天以上就返回标准时间 timeStr = "前天 " + getMinTime(time); } else if (timeGap > 24 * 60 * 60) {// 1天-2天 timeStr = timeGap / (24 * 60 * 60) + "昨天 " + getMinTime(time); } else if (timeGap > 60 * 60) {// 1小时-24小时 timeStr = timeGap / (60 * 60) + "今天 " + getMinTime(time); } else if (timeGap > 60) {// 1分钟-59分钟 timeStr = timeGap / 60 + "今天 " + getMinTime(time); } else {// 1秒钟-59秒钟 timeStr = "今天 " + getMinTime(time); } return timeStr; } public static String getChatTime(long time) { return getMinTime(time); } public static String getPrefix(long time) { long currentSeconds = System.currentTimeMillis(); long timeGap = currentSeconds - time;// 与现在时间差 String timeStr = null; L.i(" " + (timeGap - (60 * 60 * 1000))); if (timeGap > 24 * 3 * 60 * 60 * 1000) { timeStr = getDayTime(time) + " " + getMinTime(time); } else if (timeGap > 24 * 2 * 60 * 60 * 1000) { timeStr = "前天 " + getMinTime(time); } else if (timeGap > 24 * 60 * 60 * 1000) { timeStr = "昨天 " + getMinTime(time); } else { timeStr = "今天 " + getMinTime(time); } return timeStr; } public static String getDayTime(long time) { SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd"); return format.format(new Date(time)); } public static String getMinTime(long time) { SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm"); return format.format(new Date(time)); } public static Date str2Date(String str, String format) { if (str == null || str.length() == 0) { return null; } if (format == null || format.length() == 0) { format = FORMAT; } Date date = null; try { SimpleDateFormat sdf = new SimpleDateFormat(format); date = sdf.parse(str); } catch (Exception e) { e.printStackTrace(); } return date; } public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss return date2Str(c, null); } public static String date2Str(Calendar c, String format) { if (c == null) { return null; } return date2Str(c.getTime(), format); } public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss return date2Str(d, null); } public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss if (d == null) { return null; } if (format == null || format.length() == 0) { format = FORMAT; } SimpleDateFormat sdf = new SimpleDateFormat(format); String s = sdf.format(d); return s; } public static long between(String timeStr1, String timeStr2) { SimpleDateFormat df = new SimpleDateFormat(OpenfireConstant.MS_FORMART); Date begin; try { begin = df.parse(timeStr1); Date end = df.parse(timeStr2); long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒 return between; } catch (ParseException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return 0; } public static String getCurDateStr() { Calendar c = Calendar.getInstance(); c.setTime(new Date()); return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND); } /** * 以友好的方式显示时间 * * @param sdate * @return */ public static String friendly_time(String sdate) { Date time = str2Date(sdate, OpenfireConstant.MS_FORMART); if (time == null) { return "Unknown"; } String ftime = ""; Calendar cal = Calendar.getInstance(); // 判断是否是同一天 String curDate = dateFormater2.get().format(cal.getTime()); String paramDate = dateFormater2.get().format(time); if (curDate.equals(paramDate)) { int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000); if (hour == 0) ftime = Math.max( (cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟前"; else ftime = hour + "小时前"; return ftime; } long lt = time.getTime() / 86400000; long ct = cal.getTimeInMillis() / 86400000; int days = (int) (ct - lt); if (days == 0) { int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000); if (hour == 0) ftime = Math.max( (cal.getTimeInMillis() - time.getTime()) / 60000, 1) + "分钟前"; else ftime = hour + "小时前"; } else if (days == 1) { ftime = "昨天"; } else if (days == 2) { ftime = "前天"; } else if (days > 2 && days <= 10) { ftime = days + "天前"; } else if (days > 10) { ftime = dateFormater2.get().format(time); } return ftime; } private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() { @Override protected SimpleDateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } }; private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() { @Override protected SimpleDateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd"); } }; }