清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtils { private static final long ONE_MINUTE = 60; private static final long ONE_HOUR = 3600; private static final long ONE_DAY = 86400; private static final long ONE_MONTH = 2592000; private static final long ONE_YEAR = 31104000; public static Calendar calendar = Calendar.getInstance(); /** * * @return yyyy-mm-dd * 2012-12-25 */ public static String getDate() { return getYear() + "-" + getMonth() + "-" + getDay(); } /** * @param format * @return * yyyy年MM月dd HH:mm * MM-dd HH:mm 2012-12-25 * */ public static String getDate(String format) { SimpleDateFormat simple = new SimpleDateFormat(format); return simple.format(calendar.getTime()); } /** * * @return yyyy-MM-dd HH:mm * 2012-12-29 23:47 */ public static String getDateAndMinute() { SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return simple.format(calendar.getTime()); } /** * * @return * yyyy-MM-dd HH:mm:ss * 2012-12-29 23:47:36 */ public static String getFullDate() { SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simple.format(calendar.getTime()); } /** * 距离今天多久 * @param date * @return * */ public static String fromToday(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); long time = date.getTime() / 1000; long now = new Date().getTime() / 1000; long ago = now - time; if (ago <= ONE_HOUR) return ago / ONE_MINUTE + "分钟前"; else if (ago <= ONE_DAY) return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE) + "分钟前"; else if (ago <= ONE_DAY * 2) return "昨天" + calendar.get(Calendar.HOUR_OF_DAY) + "点" + calendar.get(Calendar.MINUTE) + "分"; else if (ago <= ONE_DAY * 3) return "前天" + calendar.get(Calendar.HOUR_OF_DAY) + "点" + calendar.get(Calendar.MINUTE) + "分"; else if (ago <= ONE_MONTH) { long day = ago / ONE_DAY; return day + "天前" + calendar.get(Calendar.HOUR_OF_DAY) + "点" + calendar.get(Calendar.MINUTE) + "分"; } else if (ago <= ONE_YEAR) { long month = ago / ONE_MONTH; long day = ago % ONE_MONTH / ONE_DAY; return month + "个月" + day + "天前" + calendar.get(Calendar.HOUR_OF_DAY) + "点" + calendar.get(Calendar.MINUTE) + "分"; } else { long year = ago / ONE_YEAR; int month = calendar.get(Calendar.MONTH) + 1;// JANUARY which is 0 so month+1 return year + "年前" + month + "月" + calendar.get(Calendar.DATE) + "日"; } } /** * 距离截止日期还有多长时间 * * @param date * @return */ public static String fromDeadline(Date date) { long deadline = date.getTime() / 1000; long now = (new Date().getTime()) / 1000; long remain = deadline - now; if (remain <= ONE_HOUR) return "只剩下" + remain / ONE_MINUTE + "分钟"; else if (remain <= ONE_DAY) return "只剩下" + remain / ONE_HOUR + "小时" + (remain % ONE_HOUR / ONE_MINUTE) + "分钟"; else { long day = remain / ONE_DAY; long hour = remain % ONE_DAY / ONE_HOUR; long minute = remain % ONE_DAY % ONE_HOUR / ONE_MINUTE; return "只剩下" + day + "天" + hour + "小时" + minute + "分钟"; } } /** * 距离今天的绝对时间 * * @param date * @return */ public static String toToday(Date date) { long time = date.getTime() / 1000; long now = (new Date().getTime()) / 1000; long ago = now - time; if (ago <= ONE_HOUR) return ago / ONE_MINUTE + "分钟"; else if (ago <= ONE_DAY) return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE) + "分钟"; else if (ago <= ONE_DAY * 2) return "昨天" + (ago - ONE_DAY) / ONE_HOUR + "点" + (ago - ONE_DAY) % ONE_HOUR / ONE_MINUTE + "分"; else if (ago <= ONE_DAY * 3) { long hour = ago - ONE_DAY * 2; return "前天" + hour / ONE_HOUR + "点" + hour % ONE_HOUR / ONE_MINUTE + "分"; } else if (ago <= ONE_MONTH) { long day = ago / ONE_DAY; long hour = ago % ONE_DAY / ONE_HOUR; long minute = ago % ONE_DAY % ONE_HOUR / ONE_MINUTE; return day + "天前" + hour + "点" + minute + "分"; } else if (ago <= ONE_YEAR) { long month = ago / ONE_MONTH; long day = ago % ONE_MONTH / ONE_DAY; long hour = ago % ONE_MONTH % ONE_DAY / ONE_HOUR; long minute = ago % ONE_MONTH % ONE_DAY % ONE_HOUR / ONE_MINUTE; return month + "个月" + day + "天" + hour + "点" + minute + "分前"; } else { long year = ago / ONE_YEAR; long month = ago % ONE_YEAR / ONE_MONTH; long day = ago % ONE_YEAR % ONE_MONTH / ONE_DAY; return year + "年前" + month + "月" + day + "天"; } } public static String getYear() { return calendar.get(Calendar.YEAR) + ""; } public static String getMonth() { int month = calendar.get(Calendar.MONTH) + 1; return month + ""; } public static String getDay() { return calendar.get(Calendar.DATE) + ""; } public static String get24Hour() { return calendar.get(Calendar.HOUR_OF_DAY) + ""; } public static String getMinute() { return calendar.get(Calendar.MINUTE) + ""; } public static String getSecond() { return calendar.get(Calendar.SECOND) + ""; } public static void main(String[] args) throws ParseException { String deadline = "2012-12-30 12:45:59"; Date date = new Date(); SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); date = simple.parse(deadline); System.out.println(DateUtils.fromDeadline(date)); String before = "2012-12-12 0:0:59"; date = simple.parse(before); System.out.println(DateUtils.fromToday(date)); System.out.println(DateUtils.getFullDate()); System.out.println(DateUtils.getDate()); } }