Package Test;import java.text.parseexception;import Java.text.simpledateformat;import java.util.Date;/** * Created by Luan on 2015/9/15. */public class prettytime{Private final static long minute = 60 * 1000;//1 min Private final static long hour = 60 * minute;//1 hours private final static long day = hour;//1 days private final static long month = * day;//Month PR Ivate final static long year = month;//years public static void main (string[] args) throws ParseException {St Ring m = "2015-09-15 09:35:01"; System.out.printf (Gettimeformattext (m)); /** * Returns the date of the literal description * @param date * @return */public static string Gettimeformattext (string date) { SimpleDateFormat f = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Date D; try {d = f.parse (date); } catch (ParseException e) {return date; } return Gettimeformattext (d); }/** * Returns the date of the text description * * @param date * @return */public static String Gettimeformattext (date date) {if (date = = null) {return null; } long diff = new Date (). GetTime ()-date.gettime (); Long r = 0; if (diff > year) {r = (diff/year); Return r + "years ago"; } if (diff > month) {r = (diff/month); return r + "months ago"; } if (diff > day) {r = (diff/day); return r + "days Ago"; } if (diff > hour) {r = (diff/hour); Return r + "hours ago"; } if (diff > minute) {r = (Diff/minute); return r + "minutes ago"; } return "just"; }}
"Java" Prettytime