Skip to content

Instantly share code, notes, and snippets.

@DhimanDasgupta
Last active July 12, 2016 14:37
Show Gist options
  • Save DhimanDasgupta/af76bf965e02f9307a5db85cb1e1fc07 to your computer and use it in GitHub Desktop.
Save DhimanDasgupta/af76bf965e02f9307a5db85cb1e1fc07 to your computer and use it in GitHub Desktop.
package PACKAGE_NAME;
import java.util.Date;
class Comment {
private final Date timestamp;
private final String message;
public Comment(Date timestamp, String message) {
this.timestamp = timestamp;
this.message = message;
}
public Date getTimestamp() {
return timestamp;
}
public String getMessage() {
}
}
package PACKAGE_NAME;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
class MockBackend {
private static final List<Comment> MOCK_DATA = new ArrayList<>();
private static final List<Comment> MOCK_DATA_ISO = new ArrayList<>();
static {
MOCK_DATA.add(new Comment(date("2016-02-19 06:00"), "Lorem ipsum dolor sit amet, ad antiopam intellegebat eum."));
MOCK_DATA.add(new Comment(date("2016-02-14 11:55"), "Est viderer mentitum ad. Saepe alienum laboramus eu vel,"));
MOCK_DATA.add(new Comment(date("2016-02-14 09:00"), "cu odio scribentur concludaturque sed, cu nihil virtute principes has."));
MOCK_DATA.add(new Comment(date("2016-02-13 12:00"), "Oblique vituperatoribus his no, an mei esse eligendi."));
MOCK_DATA.add(new Comment(date("2016-02-06 12:00"), "Atqui civibus cu eam, an magna maluisset mei."));
MOCK_DATA.add(new Comment(date("2016-01-14 18:40"), "Eu usu erat iisque, id vel sale everti."));
MOCK_DATA.add(new Comment(date("2016-01-13 18:40"), "Ut etiam comprehensam signiferumque his, mel"));
MOCK_DATA.add(new Comment(date("2015-10-14 18:40"), "an inani laudem eruditi, nec cu putant admodum corrumpit."));
MOCK_DATA.add(new Comment(date("2014-02-14 18:40"), "No maiorum dissentias consectetuer eos, at animal volutpat quaerendum duo."));
MOCK_DATA.add(new Comment(date("2014-02-14 18:40"), "Aeque commune ne vel."));
MOCK_DATA.add(new Comment(date("2014-02-14 18:40"), "Ferri quidam nam ad. Eros temporibus ea duo."));
//Iso Data Template.
MOCK_DATA_ISO.add(new Comment(dateIso("2016-02-19T06:00:03Z"), "Lorem ipsum dolor sit amet, ad antiopam intellegebat eum."));
MOCK_DATA_ISO.add(new Comment(dateIso("2016-02-14T11:55:55Z"), "Est viderer mentitum ad. Saepe alienum laboramus eu vel,"));
MOCK_DATA_ISO.add(new Comment(dateIso("2016-02-14T09:00:01Z"), "cu odio scribentur concludaturque sed, cu nihil virtute principes has."));
MOCK_DATA_ISO.add(new Comment(dateIso("2016-02-13T12:00:05Z"), "Oblique vituperatoribus his no, an mei esse eligendi."));
MOCK_DATA_ISO.add(new Comment(dateIso("2016-02-06T12:00:30Z"), "Atqui civibus cu eam, an magna maluisset mei."));
MOCK_DATA_ISO.add(new Comment(dateIso("2016-01-14T18:40:10Z"), "Eu usu erat iisque, id vel sale everti."));
MOCK_DATA_ISO.add(new Comment(dateIso("2016-01-13T18:40:20Z"), "Ut etiam comprehensam signiferumque his, mel"));
MOCK_DATA_ISO.add(new Comment(dateIso("2015-10-14T18:40:15Z"), "an inani laudem eruditi, nec cu putant admodum corrumpit."));
MOCK_DATA_ISO.add(new Comment(dateIso("2014-02-14T18:40:50Z"), "No maiorum dissentias consectetuer eos, at animal volutpat quaerendum duo."));
MOCK_DATA_ISO.add(new Comment(dateIso("2014-02-14T18:40:01Z"), "Aeque commune ne vel."));
MOCK_DATA_ISO.add(new Comment(dateIso("2014-02-14T18:40:03Z"), "Ferri quidam nam ad. Eros temporibus ea duo."));
}
private static Date date(String string) {
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.UK).parse(string);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
private static Date dateIso(String string){
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault());
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return simpleDateFormat.parse(string);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
public static List<Comment> loadComments() {
return MOCK_DATA;
}
public static List<Comment> loadIsoComments() {
return MOCK_DATA_ISO;
}
}
package PACKAGE_NAME;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Months;
import org.joda.time.Weeks;
import org.joda.time.Years;
class TimeStampFormatter {
/**
* For use with java.util.Date
*/
public String format(Date timestamp) {
long millisFromNow = getMillisFromNow(timestamp);
long minutesFromNow = TimeUnit.MILLISECONDS.toMinutes(millisFromNow);
if (minutesFromNow < 1) {
return "just now";
}
long hoursFromNow = TimeUnit.MILLISECONDS.toHours(millisFromNow);
if (hoursFromNow < 1) {
return formatMinutes(minutesFromNow);
}
long daysFromNow = TimeUnit.MILLISECONDS.toDays(millisFromNow);
if (daysFromNow < 1) {
return formatHours(hoursFromNow);
}
long weeksFromNow = TimeUnit.MILLISECONDS.toDays(millisFromNow) / 7;
if (weeksFromNow < 1) {
return formatDays(daysFromNow);
}
long monthsFromNow = TimeUnit.MILLISECONDS.toDays(millisFromNow) / 30;
if (monthsFromNow < 1) {
return formatWeeks(weeksFromNow);
}
long yearsFromNow = TimeUnit.MILLISECONDS.toDays(millisFromNow) / 365;
if (yearsFromNow < 1) {
return formatMonths(monthsFromNow);
}
return formatYears(yearsFromNow);
}
private long getMillisFromNow(Date commentedAt) {
long commentedAtMillis = commentedAt.getTime();
long nowMillis = System.currentTimeMillis();
return nowMillis - commentedAtMillis;
}
/**
* For use with org.joda.DateTime
*/
public String format(DateTime commentedAt) {
DateTime now = DateTime.now();
Minutes minutesBetween = Minutes.minutesBetween(commentedAt, now);
if (minutesBetween.isLessThan(Minutes.ONE)) {
return "just now";
}
Hours hoursBetween = Hours.hoursBetween(commentedAt, now);
if (hoursBetween.isLessThan(Hours.ONE)) {
return formatMinutes(minutesBetween.getMinutes());
}
Days daysBetween = Days.daysBetween(commentedAt, now);
if (daysBetween.isLessThan(Days.ONE)) {
return formatHours(hoursBetween.getHours());
}
Weeks weeksBetween = Weeks.weeksBetween(commentedAt, now);
if (weeksBetween.isLessThan(Weeks.ONE)) {
return formatDays(daysBetween.getDays());
}
Months monthsBetween = Months.monthsBetween(commentedAt, now);
if (monthsBetween.isLessThan(Months.ONE)) {
return formatWeeks(weeksBetween.getWeeks());
}
Years yearsBetween = Years.yearsBetween(commentedAt, now);
if (yearsBetween.isLessThan(Years.ONE)) {
return formatMonths(monthsBetween.getMonths());
}
return formatYears(yearsBetween.getYears());
}
private String formatMinutes(long minutes) {
return format(minutes, " minute ago", " minutes ago");
}
private String formatHours(long hours) {
return format(hours, " hour ago", " hours ago");
}
private String formatDays(long days) {
return format(days, " day ago", " days ago");
}
private String formatWeeks(long weeks) {
return format(weeks, " week ago", " weeks ago");
}
private String formatMonths(long months) {
return format(months, " month ago", " months ago");
}
private String formatYears(long years) {
return format(years, " year ago", " years ago");
}
private String format(long hand, String singular, String plural) {
if (hand == 1) {
return hand + singular;
} else {
return hand + plural;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment