Java tutorial
//package com.java2s; import java.util.Date; import java.util.concurrent.TimeUnit; public class Main { /** * date into display date * * @serverDateStr server date in string format * @return display time (Like 1 second ago, 2 months ago etc.) * */ public static String convertDateToDisplayFormat(Date serverDate) { // current date Date endDate = new Date(); long different = endDate.getTime() - serverDate.getTime(); long seconds = TimeUnit.MILLISECONDS.toSeconds(different); long minutes = TimeUnit.MILLISECONDS.toMinutes(different); long hrs = TimeUnit.MILLISECONDS.toHours(different); long days = TimeUnit.MILLISECONDS.toDays(different); int months = (int) Math.floor(days / 30); int years = (int) Math.floor(months / 12); if (years != 0) { if (years == 1) { return "1 year ago"; } else { return years + " years ago"; } } else if (months != 0) { if (months == 1) { return "1 month ago"; } else { return months + " months ago"; } } else if (days != 0) { if (days == 1) { return "Yesterday"; } else { return days + " Days ago"; } } else if (hrs != 0) { if (hrs == 1) { return hrs + " hour ago"; } else { return hrs + " hours ago"; } } else if (minutes != 0) { if (minutes == 1) { return minutes + " minute ago"; } else { return minutes + " minutes ago"; } } else { if (seconds == 0) { return "Now"; } else if (seconds == 1) { return "1 sec ago"; } else if (seconds > 0) { return seconds + " seconds ago"; } else { return "Now"; } } } }