import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created by wuqiqi on 15-11-14. */ public class TimestampUtils { private static long day = 7; /** * 获得当前时间戳 * * @return */ public static String getTimestamp() { String unixTimeGMT; try { long unixTime = System.currentTimeMillis(); unixTimeGMT = unixTime + ""; } catch (Exception e) { // TODO: handle exception unixTimeGMT = ""; } return unixTimeGMT; } /** * 获得当前时间戳 * * @return */ public static long getIntTimestamp() { long unixTimeGMT = 0; try { unixTimeGMT = System.currentTimeMillis(); } catch (Exception e) { // TODO: handle exception } return unixTimeGMT; } /** * 返回时间戳间隔 * * @return */ public static boolean compareTimestamp(long currentTimestap, long oldTimestap) { Boolean isExceed = false; if (gapTimestamp(currentTimestap, oldTimestap) > 86400 * day) { isExceed = true; } return isExceed; } public static long gapTimestamp(long currentTimestap, long oldTimestap) { return (currentTimestap - oldTimestap); } /** * 对时间戳格式进行格式化,保证时间戳长度为13位 * * @param timestamp 时间戳 * @return 返回为13位的时间戳 */ public static String formatTimestamp(String timestamp) { if (timestamp == null || "".equals(timestamp)) { return ""; } String tempTimeStamp = timestamp + "00000000000000"; StringBuffer stringBuffer = new StringBuffer(tempTimeStamp); return tempTimeStamp = stringBuffer.substring(0, 13); } /** * 根据 timestamp 生成各类时间状态串 * * @param timestamp 距1970 00:00:00 GMT的秒数 * @param format 格式 * @return 时间状态串(如:刚刚5分钟前) */ public static String getTimeState(String timestamp, String format) { if (timestamp == null || "".equals(timestamp) || timestamp.startsWith("0000-00-00")) { return ""; } SimpleDateFormat dateFormat = new SimpleDateFormat(format); Date dt = null;// 转换成功的Date对象 try { dt = dateFormat.parse(timestamp); Long time = dt.getTime();// 这就是距离1970年1月1日0点0分0秒的毫秒数 timestamp = formatTimestamp(String.valueOf(time)); long _timestamp = Long.parseLong(timestamp); if (System.currentTimeMillis() - _timestamp < 1 * 60 * 1000) { return "刚刚"; } else if (System.currentTimeMillis() - _timestamp < 30 * 60 * 1000) { return ((System.currentTimeMillis() - _timestamp) / 1000 / 60) + "分钟前"; } else { Calendar now = Calendar.getInstance(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(_timestamp); Date nowDate = now.getTime(); Date mutiDate = c.getTime(); int dayBetween = TimeUtils.daysBetween(nowDate, mutiDate); if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE)) { SimpleDateFormat sdf = new SimpleDateFormat("今天 HH:mm"); return sdf.format(c.getTime()); } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) { SimpleDateFormat sdf = new SimpleDateFormat("昨天 HH:mm"); return sdf.format(c.getTime()); } else if (dayBetween < 31) { return dayBetween + "天前"; } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) { SimpleDateFormat sdf = null; sdf = new SimpleDateFormat("M月d日 HH:mm"); return sdf.format(c.getTime()); } else { SimpleDateFormat sdf = null; if (format != null && !format.equalsIgnoreCase("")) { sdf = new SimpleDateFormat(format); } else { sdf = new SimpleDateFormat("yyyy年M月d日 HH:mm"); } return sdf.format(c.getTime()); } } } catch (Exception e) { e.printStackTrace(); return ""; } } public static String getTimeStateByFormat(String timestamp, String format) { if (timestamp == null || "".equals(timestamp) || timestamp.startsWith("0000-00-00")) { return ""; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dt = null;// 转换成功的Date对象 try { dt = dateFormat.parse(timestamp); Long time = dt.getTime();// 这就是距离1970年1月1日0点0分0秒的毫秒数 timestamp = formatTimestamp(String.valueOf(time)); long _timestamp = Long.parseLong(timestamp); if (System.currentTimeMillis() - _timestamp < 1 * 60 * 1000) { return "刚刚"; } else if (System.currentTimeMillis() - _timestamp < 30 * 60 * 1000) { return ((System.currentTimeMillis() - _timestamp) / 1000 / 60) + "分钟前"; } else { Calendar now = Calendar.getInstance(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(_timestamp); Date nowDate = now.getTime(); Date mutiDate = c.getTime(); int dayBetween = TimeUtils.daysBetween(nowDate, mutiDate); if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE)) { SimpleDateFormat sdf = new SimpleDateFormat("今天 HH:mm"); return sdf.format(c.getTime()); } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) { SimpleDateFormat sdf = new SimpleDateFormat("昨天 HH:mm"); return sdf.format(c.getTime()); } else if (dayBetween < 31) { return dayBetween + "天前"; } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) { SimpleDateFormat sdf = null; sdf = new SimpleDateFormat("M月d日 HH:mm"); return sdf.format(c.getTime()); } else { SimpleDateFormat sdf = null; if (format != null && !format.equalsIgnoreCase("")) { sdf = new SimpleDateFormat(format); } else { sdf = new SimpleDateFormat("yyyy年M月d日 HH:mm"); } return sdf.format(c.getTime()); } } } catch (Exception e) { e.printStackTrace(); return ""; } } public static String getTimeStateByNearby(String timestamp, String format) { if (timestamp == null || "".equals(timestamp) || timestamp.startsWith("0000-00-00")) { return ""; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dt = null;// 转换成功的Date对象 try { dt = dateFormat.parse(timestamp); Long time = dt.getTime();// 这就是距离1970年1月1日0点0分0秒的毫秒数 timestamp = formatTimestamp(String.valueOf(time)); long _timestamp = Long.parseLong(timestamp); if (System.currentTimeMillis() - _timestamp < 10 * 60 * 1000) { return "刚刚"; } else if ((System.currentTimeMillis() - _timestamp >= 10 * 60 * 1000) && (System.currentTimeMillis() - _timestamp < 60 * 60 * 1000)){ return ((System.currentTimeMillis() - _timestamp) / 1000 / 60) + "分钟前"; } else if ((System.currentTimeMillis() - _timestamp >= 60 * 60 * 1000) && (System.currentTimeMillis() - _timestamp < 24 * 60 * 60 * 1000)){ return ((System.currentTimeMillis() - _timestamp) / 1000 / 60 / 60) + "小时前"; }else { Calendar now = Calendar.getInstance(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(_timestamp); Date nowDate = now.getTime(); Date mutiDate = c.getTime(); int dayBetween = TimeUtils.daysBetween(nowDate, mutiDate); if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) { SimpleDateFormat sdf = new SimpleDateFormat("昨天"); return sdf.format(c.getTime()); } else if (dayBetween >= 2 && dayBetween < 7) { return dayBetween + "天前"; } else { return "7天前"; } } } catch (Exception e) { e.printStackTrace(); return ""; } } /** * v5.0圈子消息提醒时间显示 * */ public static String getTimeStateByCircleMessage(String timestamp, String format) { if (timestamp == null || "".equals(timestamp) || timestamp.startsWith("0000-00-00")) { return ""; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dt = null;// 转换成功的Date对象 try { dt = dateFormat.parse(timestamp); Long time = dt.getTime();// 这就是距离1970年1月1日0点0分0秒的毫秒数 timestamp = formatTimestamp(String.valueOf(time)); long _timestamp = Long.parseLong(timestamp); if (System.currentTimeMillis() - _timestamp < 10 * 60 * 1000) { return "刚刚"; } else if ((System.currentTimeMillis() - _timestamp >= 10 * 60 * 1000) && (System.currentTimeMillis() - _timestamp < 60 * 60 * 1000)){ return ((System.currentTimeMillis() - _timestamp) / 1000 / 60) + "分钟前"; } else if ((System.currentTimeMillis() - _timestamp >= 60 * 60 * 1000) && (System.currentTimeMillis() - _timestamp < 24 * 60 * 60 * 1000)){ return ((System.currentTimeMillis() - _timestamp) / 1000 / 60 / 60) + "小时前"; }else { Calendar now = Calendar.getInstance(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(_timestamp); Date nowDate = now.getTime(); Date mutiDate = c.getTime(); int dayBetween = TimeUtils.daysBetween(nowDate, mutiDate); if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) { SimpleDateFormat sdf = new SimpleDateFormat("昨天"); return sdf.format(c.getTime()); } else if (dayBetween < 31) { return dayBetween + "天前"; } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) { SimpleDateFormat sdf = null; sdf = new SimpleDateFormat("M月d日"); return sdf.format(c.getTime()); } else { SimpleDateFormat sdf = null; if (format != null && !format.equalsIgnoreCase("")) { sdf = new SimpleDateFormat(format); } else { sdf = new SimpleDateFormat("yyyy年M月d日"); } return sdf.format(c.getTime()); } } } catch (Exception e) { e.printStackTrace(); return ""; } } public static String getTime(String timestamp, String format) { if (timestamp == null || "".equals(timestamp) || timestamp.startsWith("0000-00-00")) { return ""; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dt = null;// 转换成功的Date对象 try { dt = dateFormat.parse(timestamp); Long time = dt.getTime();// 这就是距离1970年1月1日0点0分0秒的毫秒数 timestamp = formatTimestamp(String.valueOf(time)); long _timestamp = Long.parseLong(timestamp); Calendar now = Calendar.getInstance(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(_timestamp); Date nowDate = now.getTime(); Date mutiDate = c.getTime(); if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE)) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); return sdf.format(c.getTime()); } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) { SimpleDateFormat sdf = new SimpleDateFormat("昨天 HH:mm"); return sdf.format(c.getTime()); } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE) - 2){ SimpleDateFormat sdf = new SimpleDateFormat("前天 HH:mm"); return sdf.format(c.getTime()); }else{ SimpleDateFormat sdf = null; sdf = new SimpleDateFormat("M月d日 HH:mm"); return sdf.format(c.getTime()); } // } } catch (Exception e) { e.printStackTrace(); return ""; } } /** * 根据 timestamp 生成各类时间状态串 * * @param timestamp 距1970 00:00:00 GMT的秒数 * @return 时间状态串(如:刚刚5分钟前) */ public static String getTimeState(String timestamp) { if (timestamp == null || "".equals(timestamp) || timestamp.startsWith("0000-00-00")) { return ""; } try { timestamp = formatTimestamp(timestamp); long _timestamp = Long.parseLong(timestamp); if (System.currentTimeMillis() - _timestamp < 1 * 60 * 1000) { return "刚刚"; } else if (System.currentTimeMillis() - _timestamp < 30 * 60 * 1000) { return ((System.currentTimeMillis() - _timestamp) / 1000 / 60) + "分钟前"; } else { Calendar now = Calendar.getInstance(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(_timestamp); if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE)) { SimpleDateFormat sdf = new SimpleDateFormat("今天 HH:mm"); return sdf.format(c.getTime()); } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) { SimpleDateFormat sdf = new SimpleDateFormat("昨天 HH:mm"); return sdf.format(c.getTime()); } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) { SimpleDateFormat sdf = new SimpleDateFormat("M月d日 HH:mm"); return sdf.format(c.getTime()); } else { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日 HH:mm"); return sdf.format(c.getTime()); } } } catch (Exception e) { e.printStackTrace(); return ""; } } /** * 根据 timestamp 生成各类时间状态串 * * @param timestamp 距1970 00:00:00 GMT的秒数 * @return 时间状态串(如:刚刚5分钟前) */ public static String getTimeState2(String timestamp) { if (timestamp == null || "".equals(timestamp) || timestamp.startsWith("0000-00-00")) { return ""; } try { timestamp = formatTimestamp(timestamp); long _timestamp = Long.parseLong(timestamp); if (System.currentTimeMillis() - _timestamp < 1 * 60 * 1000) { return "刚刚"; } else if (System.currentTimeMillis() - _timestamp < 30 * 60 * 1000) { return ((System.currentTimeMillis() - _timestamp) / 1000 / 60) + "分钟前"; } else { Calendar now = Calendar.getInstance(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(_timestamp); if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH) && c.get(Calendar.DATE) == now.get(Calendar.DATE)) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); return sdf.format(c.getTime()); } else { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d"); return sdf.format(c.getTime()); } } } catch (Exception e) { e.printStackTrace(); return ""; } } /** * 判断两个时间间隔是不是大于5分钟 */ public static boolean compareTimestamp5Min(long currentTimestap, long oldTimestap) { return Math.abs((currentTimestap - oldTimestap) / (60 * 1000)) >= 5; } public static float compareTimestampHour(long currentTimestap, long oldTimestap) { return Math.abs((currentTimestap - oldTimestap) / (1.0f*60*60 * 1000)); } /** * 按格式获得时间字符串 * @param time 时间戳 * @param format 时间格式 * @return */ public static String getTimesIntToStr(long time, String format) { Date date = new Date(time); try { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } catch (Exception e) { } return ""; } /** * 按格式获得时间date * @param time 时间戳 * @param format 时间格式 * @return */ public static Date getDateFormStr(String time, String format){ Date date=null; try { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.parse(time); } catch (Exception e) { } return date; } public static String getTimeStrToFormat(String time, String format){ Date date = getDateFormStr(time, "yyyy-MM-dd HH:mm:ss"); try { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } catch (Exception e) { } return ""; } public static String getWeekForLongTime(long time){ Calendar calendar=Calendar.getInstance(); calendar.setTimeInMillis(time); int curday = calendar.get(Calendar.DAY_OF_WEEK) - 1; switch (curday){ case 0: return "星期日"; case 1: return "星期一"; case 2: return "星期二"; case 3: return "星期三"; case 4: return "星期四"; case 5: return "星期五"; case 6: return "星期六"; } return ""; } public static String getWeekForLongTime2(long time){ Calendar calendar=Calendar.getInstance(); calendar.setTimeInMillis(time); int curday = calendar.get(Calendar.DAY_OF_WEEK) - 1; switch (curday){ case 0: return "周日"; case 1: return "周一"; case 2: return "周二"; case 3: return "周三"; case 4: return "周四"; case 5: return "周五"; case 6: return "周六"; } return ""; } public static String getMonthForStrTime(String date,String format){ Calendar calendar=Calendar.getInstance(); calendar.setTimeInMillis(getDateFormStr(date,format).getTime()); int month = calendar.get(Calendar.MONTH); switch (month){ case 0: return "一月"; case 1: return "二月"; case 2: return "三月"; case 3: return "四月"; case 4: return "五月"; case 5: return "六月"; case 6: return "七月"; case 7: return "八月"; case 8: return "九月"; case 9: return "十月"; case 10: return "十一"; case 11: return "十二"; } return ""; } /** * 根据一个指定的时间戳 和当前时间戳 求相差值 换算成 x天x小时x分钟x秒 * @param timestamp 指定的时间 */ public static String getDifferenceToSecond(long timestamp){ StringBuffer sb=new StringBuffer(); long days = timestamp / (1000 * 60 * 60 * 24);// 天数 long hours = (timestamp-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);// 小时 long minutes = (timestamp-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);// 分钟 long second= timestamp/1000%60;// 秒 if(days>0){ sb.append(days+"天"); } if(hours>0){ sb.append(hours+"小时"); } if(minutes>0){ sb.append(minutes+"分"); } if(second>0){ sb.append(second+"秒"); } return sb.toString(); } /** * 根据一个指定的时间戳 换算成 x小时x分钟x秒 * @param timestamp 指定的时间 */ public static String gettimestampToHint(long timestamp){ StringBuffer sb=new StringBuffer(); long hours = timestamp/(1000* 60 * 60);// 小时 long minutes = (timestamp-hours*(1000* 60 * 60))/(1000* 60);// 分钟 long second= timestamp/1000%60;// 秒 String hoursF; String minutesF; String secondF; if (hours < 10){ hoursF = "0" + hours; }else{ hoursF = "" + hours; } if (minutes < 10){ minutesF = "0" + minutes; }else { minutesF = "" + minutes; } if (second < 10){ secondF = "0" + second; }else{ secondF = "" + second; } sb.append(hoursF+":" + minutesF + ":" + secondF); return sb.toString(); } /** * 日期 转换成时间戳 * @param date * @return */ public static long date2Timestamp(String date){ //"2015-09-18 00:00:00 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); Date d; try { d=sdf.parse(date); long l = d.getTime(); return l; } catch (ParseException e) { e.printStackTrace(); } return 0; } }
Android实用工具之一—时间戳工具
最新推荐文章于 2023-07-15 11:52:44 发布