/** * 获取当前时间 */ public static String getCurrentTime(String format) { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat(format); String dateString = formatter.format(currentTime); return dateString; }
/** * 获取时间 24小时候内 秒 分 钟 * * @param publish_Time * @return */ public static String getTimeDiff(long publish_Time) { long currentTimeMillis = System.currentTimeMillis(); long diff = 0; String str = ""; diff = currentTimeMillis - publish_Time; if (diff > 86400000)// 24 * 60 * 60 * 1000=86400000 毫秒 { Date date = new Date(publish_Time); SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "MM-dd HH:mm"); String time = simpleDateFormat.format(date); return time; } else { if (diff > 3600000) {// 60 * 60 * 1000=3600000 毫秒 // System.out.println("X小时前"); str = (int) Math.floor(diff / 3600000f) + "小时"; } else if (diff > 60000) {// 1 * 60 * 1000=60000 毫秒 // System.out.println("X分钟前"); str = (int) Math.floor(diff / 60000) + "分钟"; } else { str = (int) Math.floor(diff / 1000) + "秒"; } } return str; }
/** * 将时间戳转为代表"距现在多久之前"的字符串 * * @return */ public static String getStandardDate(Long time) { StringBuffer sb = new StringBuffer(); long mill = (long) Math.ceil(time / 1000);//秒前 long minute = (long) Math.ceil(time / 60 / 1000.0f);// 分钟前 long hour = (long) Math.ceil(time / 60 / 60 / 1000.0f);// 小时 long day = (long) Math.ceil(time / 24 / 60 / 60 / 1000.0f);// 天前 if (day - 1 > 0) { sb.append(day + "天"); } else if (hour - 1 > 0) { if (hour >= 24) { sb.append("1天"); } else { sb.append(hour + "小时"); } } else if (minute - 1 > 0) { if (minute == 60) { sb.append("1小时"); } else { sb.append(minute + "分钟"); } } else if (mill - 1 > 0) { if (mill == 60) { sb.append("1分钟"); } else { sb.append(mill + "秒"); } } else { sb.append("刚刚"); } if (!sb.toString().equals("刚刚")) { sb.append("前"); } return sb.toString(); } /** * 将时间戳转为代表"日时分秒"的字符串 * * @return */ public static String getCommonDate(Long time) { StringBuffer sb = new StringBuffer(); long day = time / (24 * 60 * 60 * 1000); if (day > 0) sb.append(day + "天"); long hour = (time / (60 * 60 * 1000) - day * 24); if (hour > 0) sb.append(hour + "小时"); long min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60); if (min > 0) sb.append(min + "分"); long s = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); if (s > 0) sb.append(s + "秒"); return sb.toString(); }
/** * 格式化时间 * @param time * @return */ public static String getCommonTime(long time) { //新的时间 String newTime = getTime(time, "yyyy-MM-dd HH:mm"); String year = newTime.substring(0, newTime.indexOf("-")); String month = newTime.substring(newTime.indexOf("-") + 1, newTime.lastIndexOf("-")); String date = newTime.substring(newTime.lastIndexOf("-") + 1, newTime.lastIndexOf(" ")); //当前时间 String currentTime = getCurrentTime("yyyy-MM-dd HH:mm"); String curretnYear = currentTime.substring(0, currentTime.indexOf("-")); String curretnMonth = currentTime.substring(currentTime.indexOf("-") + 1, currentTime.lastIndexOf("-")); String currentDate = currentTime.substring(currentTime.lastIndexOf("-") + 1, currentTime.lastIndexOf(" ")); if (year.equals(curretnYear)) {//年相同 if (month.equals(curretnMonth)) {//月相同 if (date.equals(currentDate)) {//日相同 return "今天 " + newTime.substring(newTime.indexOf(" ") + 1); } else { int i = Integer.parseInt(currentDate) - Integer.parseInt(date); if (i == 1) return "昨天 " + newTime.substring(newTime.indexOf(" ") + 1); else return newTime.substring(newTime.indexOf("-")+1);//月相同日不相同} } } else return newTime.substring(newTime.indexOf("-")+1); } else return newTime; }
/** * 得到指定月的天数 * */ public static int getMonthLastDay(String year, String month) { Calendar a = Calendar.getInstance(); a.set(Calendar.YEAR, Integer.parseInt(year)); a.set(Calendar.MONTH, Integer.parseInt(month) - 1); a.set(Calendar.DATE, 1);//把日期设置为当月第一天 a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天 int maxDate = a.get(Calendar.DATE); return maxDate; }