Created
May 16, 2018 16:08
-
-
Save pagetronic/b8c9b5d460399b181059a57e733d386a to your computer and use it in GitHub Desktop.
Date in "Since" format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.agroneo.web; | |
public class Since { | |
public static String since(long durationInit, String lng, int level) { | |
double DAYS_PER_YEAR = 365.24225D; | |
double M_PER_SECOND = 1000D; | |
double M_PER_MINUTE = 60D * M_PER_SECOND; | |
double M_PER_HOUR = 60D * M_PER_MINUTE; | |
double M_PER_DAY = 24D * M_PER_HOUR; | |
double M_PER_WEEKS = 7D * M_PER_DAY; | |
double M_PER_MONTH = Math.floor((DAYS_PER_YEAR / 12D) * M_PER_DAY); | |
double M_PER_YEAR = Math.floor(DAYS_PER_YEAR * M_PER_DAY); | |
if (durationInit < 60000D && durationInit > -60000D) { | |
return Language.getLang(durationInit > 0 ? "JUST_NOW" : "IMMEDIATELY", lng); | |
} | |
double durationMillis = (durationInit > 0) ? durationInit : -durationInit; | |
double yearsD = Math.floor(durationMillis / M_PER_YEAR); | |
durationMillis = durationMillis - (yearsD * M_PER_YEAR); | |
double monthsD = Math.floor(durationMillis / M_PER_MONTH); | |
durationMillis = durationMillis - (monthsD * M_PER_MONTH); | |
double weeksD = Math.floor(durationMillis / M_PER_WEEKS); | |
durationMillis = durationMillis - (weeksD * M_PER_WEEKS); | |
double daysD = Math.floor(durationMillis / M_PER_DAY); | |
durationMillis = durationMillis - (daysD * M_PER_DAY); | |
double hoursD = Math.floor(durationMillis / M_PER_HOUR); | |
durationMillis = durationMillis - (hoursD * M_PER_HOUR); | |
double minutesD = Math.floor(durationMillis / M_PER_MINUTE); | |
durationMillis = durationMillis - (minutesD * M_PER_MINUTE); | |
double secondsD = Math.floor(durationMillis / M_PER_SECOND); | |
durationMillis = durationMillis - (secondsD * M_PER_SECOND); | |
int years = (int) yearsD; | |
int months = (int) monthsD; | |
int weeks = (int) weeksD; | |
int days = (int) daysD; | |
int hours = (int) hoursD; | |
int minutes = (int) minutesD; | |
int seconds = (int) secondsD; | |
// years + "/" + months + "/" + weeks + "/" + days + "/" + hours + "/" + | |
// minutes + "/" + seconds; | |
StringWriter since = new StringWriter(); | |
String space_num = " "; | |
String space = ""; | |
since.append(Language.getLang(durationInit > 0 ? "AGO_BEFORE" : "IN_BEFORE", lng)); | |
for (int i = 0; i < 6 && level > 0; i++) { | |
boolean effect = false; | |
if (years > 0) { | |
since.append(space + years + space_num + (years > 1 ? Language.getLang("YEARS", lng) : Language.getLang("YEAR", lng))); | |
years = 0; | |
effect = true; | |
level--; | |
} else if (months > 0) { | |
since.append(space + months + space_num + (months > 1 ? Language.getLang("MONTHS", lng) : Language.getLang("MONTH", lng))); | |
months = 0; | |
effect = true; | |
level--; | |
} else if (weeks > 0) { | |
since.append(space + weeks + space_num + (weeks > 1 ? Language.getLang("WEEKS", lng) : Language.getLang("WEEK", lng))); | |
weeks = 0; | |
effect = true; | |
level--; | |
} else if (days > 0) { | |
since.append(space + days + space_num + (days > 1 ? Language.getLang("DAYS", lng) : Language.getLang("DAY", lng))); | |
days = 0; | |
effect = true; | |
level--; | |
} else if (hours > 0) { | |
since.append(space + hours + space_num + (hours > 1 ? Language.getLang("HOURS", lng) : Language.getLang("HOUR", lng))); | |
hours = 0; | |
effect = true; | |
level--; | |
} else if (minutes > 0) { | |
since.append(space + minutes + space_num + (minutes > 1 ? Language.getLang("MINUTES", lng) : Language.getLang("MINUTE", lng))); | |
minutes = 0; | |
effect = true; | |
level--; | |
} else if (seconds > 0) { | |
since.append(space + seconds + space_num + (seconds > 1 ? Language.getLang("SECONDS", lng) : Language.getLang("SECOND", lng))); | |
seconds = 0; | |
effect = true; | |
level--; | |
} | |
if (effect) { | |
if (level == 1) { | |
space = " " + Language.getLang("AND", lng) + " "; | |
} else { | |
space = ", "; | |
} | |
} | |
} | |
return since.toString() + Language.getLang(durationInit > 0 ? "AGO_AFTER" : "IN_AFTER", lng); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment