java提示几天前
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TimeFormatUtil { public static String getInterval(Date createAt) { // 定义最终返回的结果字符串。 String interval = null; long millisecond = new Date().getTime() - createAt.getTime(); long second = millisecond / 1000; if (second = 30 && second < 60) { interval = "半分钟前"; } else if (second >= 60 && second < 60 * 60) {//大于1分钟 小于1小时 long minute = second / 60; interval = minute + "分钟前"; } else if (second >= 60 * 60 && second < 60 * 60 * 24) {//大于1小时 小于24小时 long hour = (second / 60) / 60; if (hour = 60 * 60 * 24 && second = 60 * 60 * 24 * 2 && second = 60 * 60 * 24 * 365) {//大于365天 interval = getFormatTime(createAt, "yyyy-MM-dd HH:mm"); } else { interval = "0"; } return interval; } public static String getFormatTime(Date date, String Sdf) { return (new SimpleDateFormat(Sdf)).format(date); } public static void main(String[] args) { try { System.out.println(getInterval(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-08-03 12:12:12"))); } catch (ParseException e) { e.printStackTrace(); } } } = 30 && second < 60) { interval = "半分钟前"; } else if (second >= 60 && second < 60 * 60) {//大于1分钟 小于1小时 long minute = second / 60; interval = minute + "分钟前"; } else if (second >= 60 * 60 && second < 60 * 60 * 24) {//大于1小时 小于24小时 long hour = (second / 60) / 60; if (hour = 60 * 60 * 24 && second = 60 * 60 * 24 * 2 && second = 60 * 60 * 24 * 365) {//大于365天 interval = getFormatTime(createAt, "yyyy-MM-dd HH:mm"); } else { interval = "0"; } return interval; } public static String getFormatTime(Date date, String Sdf) { return (new SimpleDateFormat(Sdf)).format(date); } public static void main(String[] args) { try { System.out.println(getInterval(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-08-03 12:12:12"))); } catch (ParseException e) { e.printStackTrace(); } } }
用户评论