Here you can find the source of toRelativeDateString(Date value)
@SuppressWarnings("deprecation") public static String toRelativeDateString(Date value)
//package com.java2s; //License from project: Apache License import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Main { @SuppressWarnings("deprecation") public static String toRelativeDateString(Date value) { if (value != null) { String result = ""; Date dateNow = new Date(); if ((dateNow.getYear() == value.getYear()) && (dateNow.getMonth() == value.getMonth())) { int daysApart = dateNow.getDate() - value.getDate(); if (daysApart >= 28) { result = "4 weeks ago"; } else if (daysApart >= 21) { result = "3 weeks ago"; } else if (daysApart >= 14) { result = "2 weeks ago"; } else if (daysApart >= 7) { result = "a week ago"; } else if (daysApart >= 2) { result = Integer.toString(daysApart) + " days ago"; } else if (daysApart >= 1) { result = "1 day ago"; } else { int hoursApart = dateNow.getHours() - value.getHours(); if (hoursApart > 1) { result = Integer.toString(hoursApart) + " hours ago"; } else { int minsApart = dateNow.getMinutes() - value.getMinutes(); if (minsApart > 10) { result = Integer.toString(minsApart) + " minutes ago"; } else { result = "a moment ago"; }/* ww w.j a v a2 s . co m*/ } } } else { result = toDateTimeString(value); } return result; } else { return ""; } } public static String toString(Date value, String format) { if (value != null) { SimpleDateFormat dateFormat = new SimpleDateFormat(format); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return dateFormat.format(value); } else { return ""; } } public static String toDateTimeString(Date value) { if (value != null) { SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return dateFormat.format(value); } else { return ""; } } }