Code Download: http://www.zuidaima.com/share/1562038902000640.htm
On the StackOverflow to find a simple, rewritten under the requirements, to share with you.
Package Com.zuidaima;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
public class Relativedateformat {private static final long One_minute = 60000L;
Private static final long one_hour = 3600000L;
Private static final long one_day = 86400000L;
Private static final long One_week = 604800000L;
private static final String One_second_ago = "seconds ago";
private static final String One_minute_ago = "Minutes ago";
private static final String One_hour_ago = "Hour Ago";
private static final String One_day_ago = "days ago";
private static final String One_month_ago = "month ago";
private static final String One_year_ago = "Years ago"; public static void Main (string[] args) throws parseexception {SimpleDateFormat format = new SimpleDateFormat ("yyy
Y-mm-dd hh:m:s ");
Date date = Format.parse ("2013-11-11 18:35:35");
SYSTEM.OUT.PRINTLN (Format (date)); public static String format (date date) {Long Delta = NEW Date (). GetTime ()-date.gettime ();
if (Delta < 1L * One_minute) {Long seconds = toseconds (delta);
return (seconds <= 0 1:seconds) + one_second_ago;
} if (Delta < 45L * One_minute) {Long minutes = tominutes (delta);
Return (minutes <= 0 1:minutes) + one_minute_ago;
} if (Delta < 24L * One_hour) {Long hours = tohours (delta);
Return (Hours <= 0 1:hours) + one_hour_ago;
} if (Delta < 48L * One_hour) {return "Yesterday";
if (Delta < 30L * One_day) {Long days = todays (delta);
Return (days <= 0 1:days) + one_day_ago;
} if (Delta < 12L * 4L * one_week) {Long months = tomonths (delta);
Return (months <= 0 1:months) + one_month_ago;
else {Long years = Toyears (delta); Return (Years <= 0? 1 : years) + One_year_ago;
private static long toseconds (long date) {return date/1000l;
private static long tominutes (long date) {return toseconds (date)/60L;
private static long tohours (long date) {return tominutes (date)/60L;
private static long todays (long date) {return tohours (date)/24L;
private static long tomonths (long date) {return todays (date)/30L;
private static long Toyears (long date) {return tomonths (date)/365L;
}
}
Need to download, you can modify your test code test.
Reprint please be sure to indicate the source