Last active
February 14, 2021 14:20
-
-
Save aslamanver/063d129dd37a2828fc51576f7a4740ab to your computer and use it in GitHub Desktop.
Human Readable Date - Java
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
import java.util.Date; | |
public class HumanDateUtils { | |
public static String durationFromNow(Date startDate) { | |
long different = System.currentTimeMillis() - startDate.getTime(); | |
long secondsInMilli = 1000; | |
long minutesInMilli = secondsInMilli * 60; | |
long hoursInMilli = minutesInMilli * 60; | |
long daysInMilli = hoursInMilli * 24; | |
long elapsedDays = different / daysInMilli; | |
different = different % daysInMilli; | |
long elapsedHours = different / hoursInMilli; | |
different = different % hoursInMilli; | |
long elapsedMinutes = different / minutesInMilli; | |
different = different % minutesInMilli; | |
long elapsedSeconds = different / secondsInMilli; | |
String output = ""; | |
if (elapsedDays > 0) output += elapsedDays + "days "; | |
if (elapsedDays > 0 || elapsedHours > 0) output += elapsedHours + " hours "; | |
if (elapsedHours > 0 || elapsedMinutes > 0) output += elapsedMinutes + " minutes "; | |
if (elapsedMinutes > 0 || elapsedSeconds > 0) output += elapsedSeconds + " seconds"; | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment